分享在C#中时间戳是如何转换的

2023-05-21,

小编给大家分享一下分享在C#中时间戳是如何转换的,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

时间戳转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#中时间戳是如何转换的的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注本站行业资讯频道!

《分享在C#中时间戳是如何转换的.doc》

下载本文的Word格式文档,以方便收藏与打印。