TimeHelper
public static class TimeHelper { public static string GetCurrentTimestamp(bool millisecond = true) { return DateTime.Now.ToTimestamp(millisecond); } public static string ToTimestamp(this DateTime dateTime, bool millisecond = true) { return dateTime.ToTimestampLong(millisecond).ToString(); } public static long ToTimestampLong(this DateTime dateTime, bool millisecond = true) { var ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); return millisecond ? Convert.ToInt64(ts.TotalMilliseconds) : Convert.ToInt64(ts.TotalSeconds); } public static DateTime ToDateTime(this string timestamp) { var tz = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)); return timestamp.Length == 13 ? tz.AddMilliseconds(Convert.ToInt64(timestamp)) : tz.AddSeconds(Convert.ToInt64(timestamp)); } }
版权声明:本文为CSDN博主「jasonsong2008」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jasonsong2008/article/details/89923283