C# Unix时间戳和时间互转


        /// 
        /// 将Unix时间戳转换为时间
        /// 
        /// 
        /// 
        public static DateTime GetDateTime(long timeStamp)
        {
            DateTime startDt = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            long lTime = timeStamp * 10000;
            TimeSpan toNow = new TimeSpan(lTime);
            return startDt.Add(toNow);
        }

        /// 
        /// 将时间转换为Unix时间戳
        /// 
        /// 
        /// 
        public static long GetUnix(DateTime time)
        {
            DateTime startDt = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            return (long)(time - startDt).TotalMilliseconds;
        }

相关