首页  >  文章  >  后端开发  >  分享在C#中时间戳是怎么转换的?

分享在C#中时间戳是怎么转换的?

零下一度
零下一度原创
2017-06-24 10:44:281684浏览

时间戳转DateTime

timestamp为10位秒级* 10000000,若为13位毫秒级*10000。

private DateTime TimestampToDateTime(long timestamp)

{

    DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));

    long lTime = timestamp * 10000000;

    TimeSpan nowTimeSpan = new TimeSpan(lTime);

    DateTime resultDateTime = dateTimeStart.Add(nowTimeSpan);

    return resultDateTime;

}

DateTime转时间戳

秒级

private int DateTimeToTimestamp(DateTime time)
{
    DateTime startDateTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(197, 1, 1));
    return Convert.ToInt32((time - startDateTime).TotalSeconds);

}

 

 

以上是分享在C#中时间戳是怎么转换的?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn