How to parse a JSON Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# 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); // ๐Ÿ‘‰๏ธ "2023-07-27T09:53:51.111Z" const date = new Date(json); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 27 2023 12:54:06

parse json date in javascript

The code for this article is available on GitHub

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 of calling the method to the Date() constructor to create a Date object from the JSON string.

# Parse a JSON Date manually

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

index.js
const json = JSON.stringify(new Date().toISOString()); console.log(json); // ๐Ÿ‘‰๏ธ "2023-07-27T09:55:15.209Z" const date = new Date(JSON.parse(json)); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 27 2023 12:55:38 // ๐Ÿ‘‡๏ธ "2023-07-27T09:55:15.209Z" console.log(date.toISOString());

parse json date manually

The code for this article is available on GitHub

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), 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).

# Getting an ISO-formatted string

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
// ๐Ÿ‘‡๏ธ "2023-07-27T09:56:46.833Z" console.log(new Date().toISOString());
The code for this article is available on GitHub

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); // ๐Ÿ‘‰๏ธ "2023-07-27T09:56:59.806Z" const date = new Date(JSON.parse(json)); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 27 2023 12:57:08

date constructor knows how to parse iso strings

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev