1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public static long GetUnixTimeStamp() { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); long timeStamp = (long)(TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow) - startTime).TotalSeconds; return timeStamp; }
public static long GetUnixTimeStamp(DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); long timeStamp = (long)(time - startTime).TotalSeconds; return timeStamp; }
public static DateTime GetDateTimeByUnix(long timeStamp) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); DateTime dt = startTime.AddSeconds(timeStamp); return dt; }
var timeStamp1= GetUnixTimeStamp(); Console.WriteLine(timeStamp1); var dt1 = GetDateTimeByUnix(timeStamp1); System.Console.WriteLine(dt1.ToString("yyyy/MM/dd HH:mm:ss"));
|