.NET中时间戳偏移的问题,UTC和北京时间

Convert.ToInt64(DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);

使用此代码获取到的时间戳通常会比实际时间早8小时,我们猜测原因可能是new DateTime(1970,1,1)返回的其实得到的是北京时间,即UTC+8的值。
而标准的时间戳是北京时间-8小时,综上,时间戳正确代码如下

Convert.ToInt64(DateTime.UTCNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);

得到正确时间

发布了3 篇原创文章 · 获赞 0 · 访问量 166

猜你喜欢

转载自blog.csdn.net/m0_46059204/article/details/104335432