Patrick Iradukunda
Patrick Iradukunda

Reputation: 679

Is javascript date object compatible to python datetime object

For example in python, a date can be something like: 2020-06-19T11:32:16.548109Z, is there a way that i can use to convert this to a javascript Date object?

Upvotes: 2

Views: 2054

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

If your date from python is serialized into an ISO 8601 date format, then you can simply pass the string into the Date constructor in JavaScript. The precision of the decimal fraction is non-standard, as it is up the the language to decide that.

// 2020-06-19T11:32:16.548Z
new Date('2020-06-19T11:32:16.548109Z').toISOString()
// Fri, 19 Jun 2020 11:32:16 GMT
new Date('2020-06-19T11:32:16.548109Z').toUTCString()

Note: In Python, the precision of timestamp epochs are to the nearest microsecond. JavaScript will just ignore those when deserializing, because it uses millisecond epochs.

From Wikipedia:

If necessary for a particular application, the standard supports the addition of a decimal fraction to the smallest time value in the representation.

Check this article out, as it explains how different languages handle ISO 8601:

ISO 8601 and Nanosecond Precision Across Languages

Upvotes: 3

Related Questions