/// <summary> /// 本区区时当前时间转换为时间戳 /// </summary> /// <param name="localTime">本区区时当前时间</param> /// <returns>时间戳</returns> public static long ConvertLocalTimeToTimestamp(DateTime localTime) { DateTime greenwishTime = localTime - TimeZone.CurrentTimeZone.GetUtcOffset(localTime);// 获取零区时时间 TimeSpan timeSpan = greenwishTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);// 计算时间间隔 long timeStamp = Convert.ToInt64(timeSpan.TotalSeconds);// 计算全部秒数,得到时间戳 return timeStamp; } /// <summary> /// 时间戳转为本区区时当前时间 /// </summary> /// <param name="timeStamp">时间戳</param> /// <returns>本区区时当前时间</returns> private static DateTime ConvertTimestampToLocalTime(long timeStamp) { TimeSpan timeSpan = new TimeSpan(Convert.ToInt64(timeStamp + "0000000"));// 计算时间间隔 DateTime greenwishTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).Add(timeSpan);// 计算零区时当前时间 DateTime localTime = TimeZone.CurrentTimeZone.ToLocalTime(greenwishTime);// 转换为本地区时当前时间 return localTime; }