Last updated: Mar 6, 2024
Reading timeยท2 min
To create a date without timezone:
Z
character from the end of the ISO string.Date()
constructor.const dateStr = '2022-07-21T09:35:31.820Z'; const date = new Date(dateStr); console.log(date); // ๐๏ธ Thu Jul 21 2022 12:35:31 GMT+0300 const result = new Date(date.toISOString().slice(0, -1)); console.log(result); // ๐๏ธ Thu Jul 21 2022 09:35:31 GMT+0300
We passed the date string to the
Date() constructor to get a
Date
object.
09:35:31
, whereas the time in the Date
object shows 12:35:31
. This is because my time zone is 3 hours ahead of Coordinated Universal time (UTC).To create a Date
without the timezone, we called the
toISOString()
method on the Date
object and removed the character Z
from the ISO string.
The Date
object shows the exact same time as the one stored in the dateStr
variable - 09:35:31
.
The toISOString
method returns an ISO 8601 formatted string that represents
the given date.
const dateStr = '2022-07-21T09:35:31.820Z'; const date = new Date(dateStr); console.log(date); // ๐๏ธ Thu Jul 21 2022 12:35:31 GMT+0300 // ๐๏ธ "2022-07-21T09:35:31.820Z" console.log(date.toISOString());
The ISO string is formatted as YYYY-MM-DDTHH:mm:ss.sssZ
.
Z
is present, the Date
is set to UTC. If the Z
is not present, it's set to local time (this only applies if the time is provided).The Z
at the end of the ISO string means UTC, in other words, an offset from
UTC of zero hours, minutes and seconds.
You can learn more about the related topics by checking out the following tutorials: