时间戳与时间相互转换(13位)(转)

 

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

 1         /// 获取时间戳
 2         /// </summary>
 3         /// <returns></returns>
 4         public static string GetTimeStamp(System.DateTime time)
 5         {
 6             long ts = ConvertDateTimeToInt(time);
 7             return ts.ToString();
 8         }
 9         /// <summary>  
10         /// 将c# DateTime时间格式转换为Unix时间戳格式  
11         /// </summary>  
12         /// <param name="time">时间</param>  
13         /// <returns>long</returns>  
14         public static long ConvertDateTimeToInt(System.DateTime time)
15         {
16             System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
17             long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
18             return t;
19         }
20         /// <summary>        
21         /// 时间戳转为C#格式时间        
22         /// </summary>        
23         /// <param name=”timeStamp”></param>        
24         /// <returns></returns>        
25         private DateTime ConvertStringToDateTime(string timeStamp)
26         {
27             DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
28             long lTime = long.Parse(timeStamp + "0000");
29             TimeSpan toNow = new TimeSpan(lTime);
30             return dtStart.Add(toNow);
31         } 

 

posted on 2017-07-22 16:37  LJD泊水  阅读(1597)  评论(0编辑  收藏  举报