Convert an ISO Date to a Timestamp using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
2 min

banner

# Convert an ISO Date to a Timestamp using JavaScript

Use the getTime() method to convert an ISO date to a timestamp.

The getTime method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.

index.js
const isoStr = '2022-07-21T09:35:31.820Z'; const date = new Date(isoStr); const timestamp = date.getTime(); console.log(timestamp); // ๐Ÿ‘‰๏ธ 165839613182

convert iso date to timestamp

The code for this article is available on GitHub

We used the Date() constructor to parse an ISO string and get a Date object.

The getTime() method returns the number of milliseconds between the 1st of January, 1970 and the given date.

The getTime method always uses UTC for time representation. In other words, the time zone of the visitor's browser doesn't change the value the method returns.

# Removing the offset from the timestamp

If you need to remove the offset from the timestamp, use this approach instead.

index.js
const isoStr1 = '2022-07-21T09:35:31.820Z'; const date = new Date(isoStr1); const timestampWithOffset = date.getTime(); const offset = date.getTimezoneOffset() * 60 * 1000; console.log(offset); // ๐Ÿ‘‰๏ธ -10800000 const timestampWithoutOffset = timestampWithOffset - offset; const dateWithOffset = new Date(timestampWithOffset); console.log(dateWithOffset); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:31 GMT+0300 const dateWithoutOffset = new Date(timestampWithoutOffset); console.log(dateWithoutOffset); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 15:35:31 GMT+0300

remove offset from timestamp

The code for this article is available on GitHub

The getTimezoneOffset() method returns the difference, in minutes, between a date (evaluated in UTC) and the same date evaluated in the visitor's local time zone.

If you get a value like -120, then the time zone offset is UTC+02.

Similarly, for a value of -60, the time zone offset is UTC+01.

We converted the offset to milliseconds and subtracted it from the UTC timestamp to get a timestamp without the offset.

# Using getTime() to avoid mutating dates

The getTime() method can also be used to copy the date and time from one Date object to another.

index.js
const isoStr = '2022-07-21T09:35:31.820Z'; const date = new Date(isoStr); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:31 GMT+0300 const timestamp = date.getTime(); console.log(timestamp); // ๐Ÿ‘‰๏ธ 165839613182 const dateCopy = new Date(timestamp); console.log(dateCopy); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:31 GMT+0300

using gettime to avoid mutating dates

The code for this article is available on GitHub

Cloning a Date object is very useful when you want to avoid mutating it in place with some of the set* methods, e.g. setHours().

You can also use the getTime method to compare dates.

index.js
const isoStr1 = '2022-07-21T09:35:31.820Z'; const isoStr2 = '2023-04-24T10:30:00.820Z'; const date1 = new Date(isoStr1); const date2 = new Date(isoStr2); console.log(date1.getTime() > date2.getTime()); // ๐Ÿ‘‰๏ธ false console.log(date1.getTime() === date2.getTime()); // ๐Ÿ‘‰๏ธ false console.log(date1.getTime() < date2.getTime()); // ๐Ÿ‘‰๏ธ true

The examples show how to compare dates using their Unix timestamp.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev