You have a string representing a date that you want to convert to a
Date object. You may want to do this so that you can manipulate the date easily or display the date in various formats. How can you do this?
Date() constructor with the
new operator to create a
Date object. The
Date() constructor can be called with a variable number of arguments and different types of arguments. One way to call it is with a
dateString argument. The
dateString argument needs to be in the ISO 8601 format:
The string that you want to parse into a
Date should match this format or a portion of this format. The “T” character separates the date from the time portion of the string. The “Z” character is the UTC offset representation. UTC is Coordinated Universal Time, which is the primary time standard used to regulate times worldwide. It is equivalent to Greenwich Mean Time (GMT). The “Z” character can also be a ”+” or ”-” character followed by a time expression to indicate local time ahead of or behind UTC.
This date format includes date-only forms, such as
YYYY-MM, and date-with-time forms, such as
YYYY-MM-DDTHH:mm:ss. Date-only strings are formatted as UTC. Date-time strings use the local time zone, so the browser’s timezone affects the parsed date.
It’s best practice to store dates and manipulate them in UTC format. You can then use different
Date methods to display the date in UTC or in a user’s local time zone, as needed. You can display a date in UTC using the
toUTCString() method. You can use the
toString() method to display a date in the user’s local time zone.
If you can, use the full date and time in your date string.
To parse a date string as UTC, append a “Z” to the end of the date:
const date1 = new Date('2023-01-14T09:00:05.123'); const date2 = new Date('2023-01-14T09:00:05.123Z'); console.log(date1.toUTCString()); // Sat, 14 Jan 2023 07:00:05 GMT console.log(date1.toString()); // Sat Jan 14 2023 09:00:05 GMT+0200 (South Africa Standard Time) console.log(date2.toUTCString()); // Sat, 14 Jan 2023 09:00:05 GMT console.log(date2.toString()); // Sat Jan 14 2023 11:00:05 GMT+0200 (South Africa Standard Time)