C# 时间戳与DateTime互转

/// <summary>
/// 转换时间为unix时间戳
/// </summary>
/// <param name="date">需要传递UTC时间,避免时区误差,例:DataTime.UTCNow</param>
/// <returns></returns>
public static double ConvertToUnixOfTime(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date - origin;
    return Math.Floor(diff.TotalSeconds);
}


public static DateTime StampToDateTime(string timeStamp)
{
    DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
    long lTime = long.Parse(timeStamp + "0000000");
    TimeSpan toNow = new TimeSpan(lTime);
    return dateTimeStart.Add(toNow);
}

 

C# 时间戳与DateTime互转

上一篇:Winform中在容器控件上显示自定义loading信息(以图片为例)


下一篇:高速开车换底盘记:Windows 与 Linux 部署都抗住了,但修车任务艰巨