zoukankan      html  css  js  c++  java
  • DateTime与long互转

    DateTime转long:

    public static long GetDateLong(object time)
            {
                DateTime epoc = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                TimeSpan delta = new TimeSpan();
    
                if (time is DateTime)
                    delta = ((DateTime)time).Subtract(epoc);
    
                else if (time is string)
                    delta = DateTime.Parse(time.ToString()).Subtract(epoc);
    
                else
                    throw new ArgumentOutOfRangeException("时间格式错误.1");
    
                if (delta.TotalMilliseconds < 0)
                    throw new ArgumentOutOfRangeException("时间格式错误.2");
    
                long ticks = (long)delta.TotalMilliseconds;
                return ticks;
            }

    long转DateTime:

    public static DateTime GetDateFromLong(long ticks)
            {
                var date = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                date = date.AddMilliseconds(ticks);
                return date;
            }

    网上常见错误:

    DateTime epoc = new DateTime(1970, 1, 1);
  • 相关阅读:
    L1-031 到底是不是太胖了
    L1-030 一帮一
    PyCharm--git配置
    websocket--python
    UDP--python
    TCP--python
    pytest--metadata
    pytest--xdist
    pytest--夹具
    pytest--变量
  • 原文地址:https://www.cnblogs.com/zhengwk/p/5547002.html
Copyright © 2011-2022 走看看