Parsing a string to a date in JavaScript

Created by Pavel HodekLinked to 9.3m issues across 290 teams


This tutorial will explain how to parse a string into a date in JavaScript. The best way to do this is to use the ISO format together with the JavaScript Date object constructor. The ISO format looks like this: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

It is important to note that just using the ISO format alone is not reliable. This is because strings are sometimes parsed as UTC and sometimes as localtime, depending on the browser vendor and version. The best practice is to store dates as UTC and make computations as UTC. To parse a date as UTC, append a Z to the end of the string - i.e.: new Date('2023-03-27T10:20:30Z'). To display a date in UTC, use .toUTCString(), and to display a date in the user's local time, use .toString().