先上一下自己弄出来的库,.NETCore 2.2环境,以前的老库不在适用了TimeZoneInfo对象要熟悉
/// <summary> /// 获取当前时间戳 /// </summary> /// <returns></returns> public static long CurrentTimestamp() { return ToUnixTimestamp(DateTime.UtcNow); } /// <summary> /// 转换成unix时间戳 /// </summary> /// <param></param> /// <returns></returns> public static long ToUnixTimestamp(DateTime value) { TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); return (long)span.TotalMilliseconds; } /// <summary> /// unit时间转换成服务器本地时间 /// </summary> /// <param></param> /// <returns></returns> public static DateTime ToLocalTime(long unixTimeStamp) { var gmt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime dt = TimeZoneInfo.ConvertTime(gmt.AddMilliseconds(unixTimeStamp), TimeZoneInfo.Local); return dt; }