How to parse a JSON Date in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Parse a JSON Date in JavaScript #

To parse a JSON date:

  1. Use the toJSON() method to get a string representation of the Date object.
  2. Pass the JSON string to the Date() constructor.
  3. The Date() constructor will parse the ISO string and will create a Date object.
index.js
const json = new Date().toJSON(); console.log(json); // 👉️ "2022-01-23T09:04:45.904Z" const date = new Date(json); console.log(date); // 👉️ Sun Jan 23 2022 11:04:45

The toJSON() method can be called on a valid Date object.

The method returns the string representation of the given date in the ISO 8601 format.

You can pass the result from calling the method to the Date() constructor to create a Date object from the JSON string.

Here is the same example, but using a more manual approach.

index.js
const json = JSON.stringify(new Date().toISOString()); console.log(json); // 👉️ "2022-01-23T09:04:45.904Z" const date = new Date(JSON.parse(json)); console.log(date); // 👉️ Sun Jan 23 2022 11:04:45 // 👇️ "2022-01-23T09:04:45.904Z" console.log(date.toISOString());

We used the toISOString() method to get a string that represents the given date in the ISO 8601 format - YYYY-MM-DDTHH:mm:ss.sssZ.

When using the toISOString method, you always get a date according to universal time.

The toJSON() method uses the toISOString() method under the hood, so the two code snippets are equivalent, with the second one being more explicit.

The toISOString method returns the result according to universal time, so the string shows a time of 09:04:45, whereas the Date object has its time set to 11:04:45.

This is because my time zone is 2 hours ahead of Coordinated Universal Time (UTC).

When it comes to storing dates, most of the time, it is advised to store a timestamp or a valid ISO 8601 string (UTC) in your database.

For example, if you store a local time of midnight (00:00) in your database, you wouldn't know if that's midnight in Tokyo (Japan), in Paris (France), in New York (US), etc. These are all different moments that are hours apart.

For consistency, you should mostly use local time when you have to render a date and time to the user, but store the actual values in UTC (=GMT).

It is quite easy to get an ISO formatted string of a Date in JavaScript. You simply have to use the toISOString() method.

index.js
// 👇️ "2022-01-23T09:24:54.995z" console.log(new Date().toISOString());

The string can easily be converted to JSON by using the JSON.stringify method and can then be transmitted over the network.

The Date() constructor knows how to parse ISO 8601 strings, so all you have to do is parse the JSON and pass the ISO string to the constructor.

index.js
const json = JSON.stringify(new Date().toISOString()); console.log(json); // 👉️ "2022-01-23T09:04:45.904Z" const date = new Date(JSON.parse(json)); console.log(date); // 👉️ Sun Jan 23 2022 11:04:45
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.