zoukankan      html  css  js  c++  java
  • 【转】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);
    短短几行代码,却花了我几个小时,就因为时区的问题,@_@
  • 相关阅读:
    多线程--同步--方法块和同步块synchronized
    CentOS7.6安装Nodejs(Npm)
    [原][译]关于osgEarth::VirtualProgram说明
    [转]opengl的学习网站
    [转]OpenGL中的功能与OSG对应功能
    [原]最简单的c语言,出错输出,日志打印 以及 C预定义的宏
    [转]netcdf入门
    [NetCDF][C++] 使用NetCDF 的接口读取数值
    [python] pip安装国外软件库(包)失败,解决方案
    [原][python]安装python,读取、遍历excel表格
  • 原文地址:https://www.cnblogs.com/nhlinkin/p/3601068.html
Copyright © 2011-2022 走看看