• 【转】UNIX时间戳与.net日期类的转换


    1 将系统时间转换成UNIX时间戳
      DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
      DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
      TimeSpan toNow = dtNow.Subtract(dtStart);
      string timeStamp = toNow.Ticks.ToString();
      timeStamp = timeStamp.Substring(0,timeStamp.Length - 7);

    2将UNIX时间戳转换成系统时间
      string timeStamp = "1144821796";
      DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
      long lTime = long.Parse(timeStamp + "0000000");
      TimeSpan toNow = new TimeSpan(lTime);
      DateTime dtResult = dtStart.Add(toNow);

    //===================================

    //DateTime转变为UNIX时间,直接转换,不改变时区
    DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0);
    TimeSpan toNow = dt.Subtract(dtStart);
    int timeStamp = (int)(toNow.Ticks/10000000);

    //===================================

     
    转自:http://blog.163.com/i_lcw/blog/static/87809734200926118577/
     
     
     
    http://cache.baiducontent.com/c?m=9d78d513d99000ed4fece4690d61c0676908d7252bd6a00209d1843be0735a31193dbffc6d351174c4b40f7071ac5e2c9be74174234460e99492ce0c9fac935b32956271350b8636488048f6911b7a9637902db8f246fac1ac66ceca89849a49208a155e2bdea79c5d7415ca64f51f26e3d1c30e4a01&p=882a9545d29407eb44b0c7710f5788&newp=8b2a975f8a9c11a058ed9066464c9d231610db2151d1d3116588cc&user=baidu&fm=sc&query=asp%2Enet%D7%AAlinux%CA%B1%BC%E4&qid=&p1=7


    1 将系统时间转换成UNIX时间戳
    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
    DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
    TimeSpan toNow = dtNow.SuBTract(dtStart);
    string timeStamp = toNow.Ticks.ToString();
    timeStamp = timeStamp.Substring(0,timeStamp.Length - 7);2将UNIX时间戳转换成系统时间
    string timeStamp = "1144821796";
    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
    long lTime = long.Parse(timeStamp + "0000000");
    TimeSpan toNow = new TimeSpan(lTime);
    DateTime dtResult = dtStart.Add(toNow);
    短短几行代码,却花了我几个小时,就因为时区的问题,@_@
  • 相关阅读:
    .NET 图片解密为BASE64
    IIS&ASP.NET 站点IP跳转到域名
    http转https实战教程iis7.5
    接口传参几种方式
    Python3 字符串
    ASP.Net Core WebApi几种版本控制对比
    Docker 部署NetCore 接口(三)
    Docker关键概念和基本命令(二)
    Windows平台下kafka环境的搭建以及简单使用
    CentOS7 安装 Docker-CE(一)
  • 原文地址:https://www.cnblogs.com/nhlinkin/p/3601068.html
Copyright © 2020-2023  润新知