int UTCSecond = (int)((DateTimeOffset)DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local)).ToUnixTimeSeconds(); DateTime time = DateTimeOffset.FromUnixTimeSeconds(UTCSecond).DateTime; Console.WriteLine("当前utc:{0},秒数:{1},换算时间:{2}", DateTimeOffset.Now, UTCSecond, time.ToString("F")); UTCSecond = Q.Helper.TimeHelper.ToUTCSecond(DateTime.Now); time = DateTimeOffset.FromUnixTimeSeconds(UTCSecond).DateTime; Console.WriteLine("当前utc:{0},秒数:{1},换算时间:{2}", DateTimeOffset.Now, UTCSecond, time.ToString("F")); Console.Read(); |
QCOMMON请使用NUGET包管理器搜索安装 QCommon
基本原理如下
/// /// UTC时间 /// static /// /// 时间转UTC秒 /// /// /// public { return (int)dateTime.ToUniversalTime().Subtract(UTC).TotalSeconds; }
/// /// UTC秒转当地时间 /// /// /// public { return UTC.AddSeconds(second).ToLocalTime(); } |