I am saving DateTimeOffset object in MongoDB. Mongo driver serilizes DateTimeOffset as following.
[635519027206007023,0]
I need it to be converted to ISO 8601 format as follows.
2015-02-02T01:43:19+05:00
Conversion can be easily done using help of .NET DateTimeOffset class. But, in this case I am retrieving DateTimeOffset directly from MongoDB using JavaScript (Node.js). Hence I end up retrieving array of long.
How can I convert array of longs [635519027206007023,0]
to ISO 8601 format 2015-02-02T01:43:19+00:00
using JavaScript
?
The epoch for the DateTimeOffset
type is 0000-01-01, while the epoch for Javascript dates is 1970-01-01.
The DateTimeOffset
ticks value for the date 1970-01-01 is 621355968000000000, so you can just subtract that from the value to convert it to the Javascript epoch.
Ticks in DateTimeOffset
is 1/10000000 second, while ticks in Javascript dates is 1/1000 second, so divide the value by 10000 to convert it to Javascript date ticks.
So, to get the Javascript date from the DateTimeOffset
ticks:
new Date((date[0] - 621355968000000000) / 10000)
Then you can use the toISOString
method to convert the date to an ISO8601 string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.