C# 时间戳转日期

 

 

/// <summary>
        /// 时间戳反转为时间,有很多中翻转方法,但是,请不要使用过字符串(string)进行操作,大家都知道字符串会很慢!
        /// </summary>
        /// <param name="TimeStamp">时间戳</param>
        /// <param name="AccurateToMilliseconds">是否精确到毫秒</param>
        /// <returns>返回一个日期时间</returns>
        public static DateTime GetTime(long TimeStamp, bool AccurateToMilliseconds = false)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            if (AccurateToMilliseconds)
            {
                return startTime.AddTicks(TimeStamp * 10000);
            }
            else
            {
                return startTime.AddTicks(TimeStamp * 10000000);
            }
        }

 

 

举例:

string timestr = "1645517610";

DateTime time =  Convert.ToDateTime(GetTime(long.Parse(timestr)));

 

 

日期与时间戳互转参考地址:

https://blog.csdn.net/u013899802/article/details/120706554

posted @ 2022-02-22 16:22  一介桃白白  阅读(2464)  评论(0编辑  收藏  举报