zoukankan      html  css  js  c++  java
  • 时间戳与时间相互转换(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         } 
  • 相关阅读:
    NuGet包介绍
    修改逻辑文件名
    检查扫描文件系统
    C# Newtonsoft.Json不序列字段
    HierarchyId通过父节点创建一个新的子节点
    常用SQL语句
    redis脚本
    asp.net 的一个简单进度条功能
    .Net C#向远程服务器Api上传文件
    使用IKVM在C#中调用JAVA程序
  • 原文地址:https://www.cnblogs.com/lijianda/p/7221751.html
Copyright © 2011-2022 走看看