C# 时间戳转日期

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <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 @   一介桃白白  阅读(2479)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2021-02-22 C# 依赖注入
2021-02-22 【转】Aspnet Core为什么支持跨平台
点击右上角即可分享
微信分享提示