遇到 Unix 时间戳转换的问题, 遂记录下来.
Unix 时间戳转 DateTime
- string UnixTime = "1474449764"; // 时间戳
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- DateTime TranslateDate = startTime.AddSeconds(double.Parse(UnixTime));
DateTime 转 Unix 时间戳
- //DateTime 转 Unix 时间戳
- DateTime DateNow = DateTime.Now;
- long UnixDate= (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
- Tips:
一个时钟周期表示 100 个纳秒的一千万分之一. 没有以毫秒为单位, 10000 个计时周期或 1000 万个计时周期一秒内.
DateTime.Ticks 属性的值表示 0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒间隔数 (0: 00:00 年 1 月 1 日 UTC 0001, 公历日历中的), 表示 DateTime.MinValue. 它不包括归因于闰秒为单位的计时周期数.
附: 这是一个时间戳的转换网址, 上面也有不同语言的转换.
来源: http://www.bubuko.com/infodetail-2770953.html