How to Convert an ISO Date String to a Timestamp in JavaScript
A frequent task in web development is converting a date string, especially one in the standard ISO 8601 format, into a numerical timestamp. This is essential for storing dates, performing date arithmetic, or reliably comparing two moments in time. The most direct and reliable way to do this in JavaScript is by parsing the string with the Date constructor and then using the .getTime() method.
This guide will teach you the correct way to get a UTC timestamp from an ISO string, clarify the crucial concept that timestamps are timezone-independent, and show you practical uses for this conversion, such as comparing dates.
The Core Concepts: ISO String vs. Timestamp
It's important to understand the two formats we are working with.
| Format | Description | Example |
|---|---|---|
| ISO 8601 String | A human-readable text representation of a date and time. The Z at the end signifies "Zulu" time, which is UTC. | '2023-10-27T14:30:00Z' |
| JavaScript Timestamp | A number representing the milliseconds that have elapsed since the Unix Epoch (00:00:00 UTC on January 1, 1970). | 1698417000000 |
A timestamp is a universal, timezone-agnostic number that represents a single, unambiguous moment in time.
The Core Method: new Date().getTime()
The process is a simple, two-step operation: first parse the string, then get the timestamp.
For example, you have an ISO date string from an API or database and need its numerical timestamp equivalent.
// Problem: How to get the numerical timestamp for this string?
const isoString = '2023-10-27T14:30:00Z';
Solution:
const isoString = '2023-10-27T14:30:00Z';
// 1. Parse the ISO string into a Date object.
const dateObject = new Date(isoString);
// 2. Get the timestamp from the Date object.
const timestamp = dateObject.getTime();
console.log(timestamp); // Output: 1698417000000
This is the standard and most reliable way to perform the conversion. The JavaScript Date constructor is specifically designed to parse ISO 8601 strings correctly.
A Critical Point: Timestamps and Time Zones
A common point of confusion is the idea of a "local timestamp" or removing an "offset" from a timestamp. This is a misunderstanding of what a timestamp is.
The value returned by date.getTime() is always UTC. It is a universal count of milliseconds since the Unix Epoch and has no concept of a time zone. It represents the same moment in time for everyone in the world, regardless of their local time.
The date.getTimezoneOffset() method does not apply to the UTC timestamp. It simply tells you the difference, in minutes, between the user's browser's local time and UTC for that specific date. You should not add or subtract this offset from a UTC timestamp if your goal is to have a universal time value.
Bonus: Converting a Timestamp Back to a Date Object
The reverse operation is just as simple. You can pass a numerical timestamp directly to the Date constructor to create a Date object from it.
const timestamp = 1698417000000;
const dateObject = new Date(timestamp);
console.log(dateObject.toUTCString()); // Output: "Fri, 27 Oct 2023 14:30:00 GMT"
This is useful for taking a timestamp from a database and converting it back into a Date object that you can then format for display.
Practical Example: Comparing Two ISO Date Strings
The most common reason to convert dates to timestamps is to compare them. Comparing numbers is far more reliable than comparing strings.
For example, how do you determine which of these two ISO date strings is later?
const iso1 = '2023-10-27T14:30:00Z';
const iso2 = '2023-10-27T15:00:00Z';
Solution: convert both to timestamps and compare the numbers.
const iso1 = '2023-10-27T14:30:00Z';
const iso2 = '2023-10-27T15:00:00Z';
const timestamp1 = new Date(iso1).getTime();
const timestamp2 = new Date(iso2).getTime();
if (timestamp1 < timestamp2) {
console.log(`${iso1} is earlier than ${iso2}`);
} else if (timestamp1 > timestamp2) {
console.log(`${iso1} is later than ${iso2}`);
} else {
console.log('The dates are the same.');
}
Output:
2023-10-27T14:30:00Z is earlier than 2023-10-27T15:00:00Z
Conclusion
Converting an ISO date string to a timestamp is a fundamental and straightforward task in JavaScript.
- The standard method is to first parse the string with
new Date(isoString)and then get the numerical value with the.getTime()method. - Remember that a JavaScript timestamp is always in UTC and is independent of any time zone. Do not attempt to add or subtract
getTimezoneOffset()from it. - The primary use for timestamps is to reliably compare, sort, or store specific moments in time.