时间戳实际就是当前时间距离1970年1月1日0点0时0分0秒(转换成北京时间是1970年1月1日8点0时0分0秒)距离你要计算的时间的秒数或者毫秒数
一般来说:我们用的时间戳到秒的话是10位,到毫秒的话是13位
北京时间 2001-09-09 09:46:40是10位起始值,毫秒的话多三位 北京时间 2286-11-21 01:46:40是11位起始值,毫秒的话多三位
/// <summary> /// 获取时间戳,为真时获取10位(秒)时间戳(Unix),为假时获取13位(毫秒)时间戳 /// </summary> /// <param name="bflag">.</param> /// <returns></returns> public static long GetTimeStamp(DateTime dt,bool bflag) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 TimeSpan ts = dt - startTime; long ret = 0; if (bflag) ret = Convert.ToInt64(ts.TotalSeconds); else ret = Convert.ToInt64(ts.TotalMilliseconds); return ret; } /// <summary> /// 将时间戳转换为DateTime时间,bSecond为true:秒,bSecond为false:毫秒 /// </summary> /// <param name="timestamp"></param> /// <param name="bSecond"></param> /// <returns></returns> public static DateTime TimeStampToDateTime(long timestamp,bool bSecond) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 if (bSecond) { return startTime.AddSeconds(timestamp); } else return startTime.AddMilliseconds(timestamp); }