C# DateTime与时间戳转换
/// <summary> /// DateTime转时间戳 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long GetTimeStamp(DateTime dateTime) { // 开始时间 DateTime startTime = new(1970, 1, 1, 8, 0, 0); // 10位的时间戳 long timeStamp = Convert.ToInt64(dateTime.Subtract(startTime).TotalSeconds); // 13位的时间戳 //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds); return timeStamp; } /// <summary> /// 时间戳转DateTime /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> public static DateTime GetDateTime(long timeStamp) { // 开始时间 DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local); long lTime = long.Parse(timeStamp + "0000000"); return startTime.Add(new TimeSpan(lTime)).AddHours(8); }