C#实现时间戳timestamp与时间相互转换

C#实现时间戳timestamp与时间相互转换

#region 日期转换数字
///
/// 将Unix时间戳转换为DateTime类型时间
///
/// double 型数字 /// DateTime
public static System.DateTime ConvertIntDateTime(double d)
{
    System.DateTime time = System.DateTime.MinValue;
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
    time = startTime.AddSeconds(d);
    return time;
}
///
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
public static double ConvertDateTimeInt(System.DateTime time)
{
    double intResult = 0;
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
    TimeSpan ts = time - startTime;
    intResult = ts.TotalSeconds;
    return intResult;
}
#endregion
Last modification:April 13, 2020
如果觉得我的文章对你有用,请随意赞赏