public class UnixTimeUtil
{
/// <summary>
/// 将dateTime格式转换为Unix时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static int DateTimeToUnixTime(DateTime dateTime)
{
return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ))).TotalSeconds;
} /// <summary>
/// 将Unix时间戳转换为dateTime格式
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static DateTime UnixTimeToDateTime(int time)
{
if (time < )
throw new ArgumentOutOfRangeException("time is out of range"); return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , )).AddSeconds(time);
}
}

还可以这样子求Unix时间戳:

(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000

原文链接:https://www.cnblogs.com/yaosj/p/11230626.html

05-08 15:11