C#时间戳和datetime相互转换

 

        /// 
        /// 获取当前时间戳
        /// 
        /// 
        public  long GetTimestamp()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;     //精确到毫秒
        }
        /// 
        /// 时间戳转datetime
        /// 
        /// 
        public  DateTime ConvertUnixToDateTime(string unix)
        {
            DateTime startUnixTime = System.TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            return startUnixTime.AddMilliseconds(double.Parse(unix));
        }

 

你可能感兴趣的:(.NET)