2

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?

3
  • This is an ISO 8601 format: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 19, 2020 at 14:27
  • JS has millisecond precision whereas python has microsecond precision, you may need to strip the last three digits to round to the nearest thousandths? But this does not seem to matter. The Date constructor can read ISO format e.g. new Date('2020-06-19T11:32:16.548109Z') Commented Jun 19, 2020 at 14:31
  • thank you soo much Commented Jun 19, 2020 at 15:09

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.