• .net日期类与UNIX时间戳的相互转换,长数字


    第一种

    //将系统时间转换成unix时间戳
            public string timeunix(string time)
            {
                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);
                return timeStamp;
            }
            //将unix时间戳转换成系统时间
            public DateTime unixtime(string time)
            {
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                long lTime = long.Parse(time + "0000000");
                TimeSpan toNow = new TimeSpan(lTime);
                DateTime dtResult = dtStart.Add(toNow);
                return dtResult;
            }

    第二种

            //将系统时间转换成unix时间戳
            public static long timeunix(DateTime dt)
            {
                DateTimeOffset dto = new DateTimeOffset(dt);
                return dto.ToUnixTimeSeconds();
            }
    
            //将系统时间转换成unix时间戳
            public static DateTime unixtime(double d)
            {
                System.DateTime time = System.DateTime.MinValue;
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                time = startTime.AddMilliseconds(d);
                return time;
            }
  • 相关阅读:
    <>跟!=
    exists查询中子表可以是
    in 与 = 的区别
    F查询和Q查询
    Django ORM常用字段和参数
    Django模板系统
    Djang视图层
    Django路由层
    ASP.NET前端语法应用
    WebConfig加密解密
  • 原文地址:https://www.cnblogs.com/wybshyy/p/16042550.html
Copyright © 2020-2023  润新知