Convert an ISO Date to a Timestamp using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Convert an ISO Date to a Timestamp using JavaScript #

Use the getTime() method to convert an ISO date to a timestamp, e.g. new Date(isoStr).getTime(). 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

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

The getTime method returns the number of milliseconds between 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.

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

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.

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

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.

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.