编程语言
首页 > 编程语言> > C# UTC转换成时间

C# UTC转换成时间

作者:互联网

 public class DateTimeUtil
    {
        private static DateTime utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        /// <summary>
        /// 转换为DateTime
        /// </summary>
        /// <param name="timeStamp">10位时间戳(单位:毫秒)</param>
        /// <returns>DateTime</returns>
        public static DateTime TimeStampToDateTime(long timeStamp)
        {
            return utcStartTime.AddMilliseconds(timeStamp).ToLocalTime();
        }

        /// <summary>
        /// DateTime转换为时间戳
        /// </summary>
        /// <param name="dateTime"> DateTime</param>
        /// <returns>10位时间戳(单位:毫秒)</returns>
        public static long DateTimeToTimeStamp(DateTime dateTime)
        {
            return (long)(dateTime.ToUniversalTime() - utcStartTime).TotalMilliseconds;
        }
    }

  

标签:UTC,转换成,C#,long,DateTime,static,utcStartTime,dateTime,public
来源: https://www.cnblogs.com/qdmb/p/16490603.html