将纳秒转换为日期时间

Converting Nanoseconds to Datetime

我在将纳秒转换为 DateTime 时遇到了一些问题,因此我可以使用 Google Fit API (https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets/get)

Dataset identifier that is a composite of the minimum data point start time and maximum data point end time represented as nanoseconds from the epoch. The ID is formatted like: "startTime-endTime" where startTime and endTime are 64 bit integers.

我可以通过这种方式将日期时间转换为纳秒

DateTime zuluTime = ssDatetime.ToUniversalTime();
DateTime unixEpoch = new DateTime(1970, 1, 1);
ssNanoSeconds = (Int32)(zuluTime.Subtract(unixEpoch)).TotalSeconds + "000000000";

但现在我需要将纳秒转换为 DateTime。我该怎么做?

Ticks 属性 表示 100 纳秒。那么怎么样:

 var ssNanoSeconds = ((zuluTime.Subtract(unixEpoch)).Ticks / 100)

从纳秒到 DateTime

 DateTime dateTime = new DateTime(1970, 1, 1).AddTicks(nanoSeconds * 100) ; 

使用AddTicks方法。不要忘记将纳秒除以 100 以获得刻度。

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