C#:如何将纳秒转换为本地DateTimeOffset

问题描述 投票:0回答:1

如何将纳秒转换为Datetimeoffset。

我尝试过约会时间

        long nanoseconds = 1449491983090000000;
        DateTime epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
        DateTime result1 = epochTime.AddTicks(nanoseconds / 100);

        DateTime epochTimfe = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        DateTime result2 = epochTime.AddTicks(nanoseconds / 100);

result1result2都给了我GMT时间。即,>

2015/12/7 12:39:43 PM

我从here验证了这一点>

谁能帮助我将纳秒转换为DateTimeOffset吗?

如何将纳秒转换为Datetimeoffset。我尝试的日期时间长纳秒= 1449491983090000000; DateTime epochTime =新的DateTime(1970,1,1,0,0,0,DateTimeKind.Local); ...

c# datetime datetimeoffset
1个回答
0
投票

您可以在UTC中构造DateTimeOffset的实例,添加纳秒,然后调用ToLocalTime以获取本地版本。

long nanoseconds = 1449491983090000000;
var epochTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
var utc = epochTime.AddTicks(nanoseconds / 100);
var local = utc.ToLocalTime();
© www.soinside.com 2019 - 2024. All rights reserved.