C# 常用时间戳处理方法
C# 常用时间戳处理方法
1 /// <summary> 2 /// 时间戳转为C#格式时间 3 /// </summary> 4 /// <param name="timeStamp">Unix时间戳格式</param> 5 /// <returns>C#格式时间</returns> 6 public static DateTime GetTime(string timeStamp) 7 { 8 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 9 long lTime = long.Parse(timeStamp + "0000000"); 10 TimeSpan toNow = new TimeSpan(lTime); 11 return dtStart.Add(toNow); 12 } 13 14 15 /// <summary> 16 /// DateTime时间格式转换为Unix时间戳格式 17 /// </summary> 18 /// <param name="time"> DateTime时间格式</param> 19 /// <returns>Unix时间戳格式</returns> 20 public static int ConvertDateTimeInt(System.DateTime time) 21 { 22 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 23 return (int)(time - startTime).TotalSeconds; 24 }