C# 时间戳和时间的相互转换

Posted LongtengGensSupreme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 时间戳和时间的相互转换相关的知识,希望对你有一定的参考价值。

时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。 

 C#格式时间转时间戳Timestamp

 private int GetCreatetime()  {

                DateTime DateStart= new DateTime(1970,1,1,8,0,0);     

               return Convert.ToInt32((DateTime.Now - DateStart).TotalSeconds);

 }  

 

时间戳转为C#格式时间 

 private DateTime GetTime(string timeStamp)  {
           DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
           long lTime = long.Parse(timeStamp + "0000000");
  •            TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);  
  • }  

以上是关于C# 时间戳和时间的相互转换的主要内容,如果未能解决你的问题,请参考以下文章

Python time和datetime时间戳和时间字符串相互转换

PHP时间戳和日期相互转换

javascript时间戳和日期字符串相互转换

javascript时间戳和日期字符串相互转换

C语言应用——Unix时间戳和北京时间的相互转换

100个 Unity实用技能 | Lua中获取当前时间戳,时间戳和时间格式相互转换时间戳转换为多久之前