Sentry Answers>JavaScript>

Parsing a string to a `Date` in JavaScript

Parsing a string to a `Date` in JavaScript

Matthew C.

The ProblemJump To Solution

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?

The Solution

Call the 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:

Click to Copy
YYYY-MM-DDTHH:mm:ss.sssZ

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:

Click to Copy
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)
  • ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns
  • ResourcesJavaScript Frontend Error Monitoring 101
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.