Matthew C.
—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?
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:
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:
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)
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.