unix时间戳和c#转换

xiaoxiao2021-02-27  399

1、时间戳转日期

public static DateTime IntToDateTime(int timestamp) { return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1)).AddSeconds(timestamp); }

调用:IntToDateTime(1458637638);

输出:2016/3/22 17:7:18

2、日期转时间戳

public static DateTime DateTimeToInt(DateTime datetime) { return (datetime.ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000 }

调用: DateTimeToInt(DateTime.Now);

输出:1458638060

转载请注明原文地址: https://www.6miu.com/read-4535.html

最新回复(0)