But strings are sometimes parsed as UTC and sometimes as local time, which is based on browser vendor and version. It is recommended is to store dates as UTC and make computations as UTC.
To parse a date as UTC, you should append a Z:
The "Z" is crucial to ensure that the string is interpreted as UTC, not as local time.
The date objects keep track of a specific instant in time, which is irrespective of timezone. If you use the Date's UTC methods, you will access that instant in UTC. In case you use non-UTC methods, you will access it in local time.
let str = "2020-04-10T17:14:00"; let date = new Date(str + "Z"); console.log("UTC string: " + date.toUTCString()); console.log("Local string: " + date.toString()); console.log("Hours local: " + date.getHours()); console.log("Hours UTC: " + date.getUTCHours());
Date and Time¶
You can use the Date object to display the current Date and time, create a calendar, build a timer, etc. The new operator is used to create a date object, and a set of methods become available to operate on the object. It allows you to set and get the year, month, day, hour, minute, second, and millisecond using either local time or UTC time.