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);
    短短几行代码,却花了我几个小时,就因为时区的问题,@_@
  • 相关阅读:
    20190729
    [51Nod1623] 完美消除
    [WC2013] 糖果公园
    20190727
    在Java中调用带参数的存储过程
    Oracle 字符集的查看和修改
    查询oracle server端的字符集
    Mysql 数据库中文乱码问题
    错误:”未能加载文件或程序集“System.Web.Mvc, Version=2.0.0.0” 解决方法
    CSS背景图拉伸自适应尺寸,全浏览器兼容
  • 原文地址:https://www.cnblogs.com/nhlinkin/p/3601068.html
Copyright © 2011-2022 走看看