C#时间格式转换为时间戳(互转)

时间:2023-01-19 02:35:03

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

/// <summary>
/// 时间戳Timestamp
/// </summary>
/// <returns></returns>
private int GetCreatetime()
{
DateTime DateStart= new DateTime(1970,1,1,8,0,0);
return Convert.ToInt32((DateTime.Now - DateStart).TotalSeconds);
}
</pre><p><pre name="code" class="csharp"> /// <summary>/// 时间戳转为C#格式时间/// </summary>/// <param name=”timeStamp”></param>/// <returns></returns>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);}