How to Get a UTC/GMT Timestamp in JavaScript
When working with dates and times, especially for server communication, logging, or comparisons, it is a crucial best practice to use a universal time standard to avoid ambiguity. The global standard for this is UTC (Coordinated Universal Time), and the most reliable way to represent a moment in time is with a UTC timestamp.
This guide will explain the critical difference between a numerical timestamp and a formatted UTC string, and teach you the standard, built-in JavaScript methods for getting both.
The Core Concepts: Timestamp vs. Formatted String
It's essential to understand the distinction between these two representations of time:
| Type | Description | Example | Use Case |
|---|---|---|---|
| Timestamp (Number) | The number of milliseconds since the Unix Epoch (Jan 1, 1970). This value is always UTC. | 1760809394097 | Storing, comparing, or performing date arithmetic. |
| Formatted String | A human-readable text representation of the date and time in UTC. | "2025-10-18T17:43:36.000Z" | Displaying, logging, or sending to an API. |
How to Get a UTC Timestamp (The Number)
A JavaScript timestamp is, by definition, a UTC-based value. It represents the same moment in time for everyone in the world, regardless of their local time zone.
For example, you want a universal, numerical value representing the current moment.
Recommended Solution: Date.now()
The Date.now() static method is the most direct way to get the current timestamp. It is highly performant and clear in its intent.
const utcTimestamp = Date.now();
console.log(utcTimestamp);
Example of Output:
1760809394097
Alternative Solution: .getTime()
If you already have a Date object, you can use the .getTime() method to get its timestamp.
const myDate = new Date();
const utcTimestamp = myDate.getTime();
console.log(utcTimestamp);
Both methods return the same value. Date.now() is slightly more performant as it doesn't create an intermediate Date object.
How to Get a UTC Formatted String
If you need a string representation of a date in UTC, JavaScript provides two excellent built-in methods.
Recommended Solution: toISOString()
The Date.prototype.toISOString() method is the modern standard. It returns the date in the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ), which is universally recognized and perfect for machine-to-machine communication (like APIs). The Z at the end signifies "Zulu" time, which is UTC.
const now = new Date();
const isoString = now.toISOString();
console.log(isoString);
Example of Output:
2025-10-18T17:45:27.553Z
Human-Readable Alternative: toUTCString()
If you need a more human-friendly format, you can use the toUTCString() method.
const now = new Date();
const utcString = now.toUTCString();
console.log(utcString);
Example of Output:
Sat, 18 Oct 2025 17:46:02 GMT
Getting Individual UTC Components
If you need to build a custom-formatted UTC date string, you must use the specific getUTC* methods on the Date object. These are the UTC equivalents of the standard get* methods.
getUTCFullYear()getUTCMonth()(0-11)getUTCDate()(1-31)getUTCHours()(0-23)
Solution:
function getCustomUtcTimestamp(date) {
const pad = (num) => num.toString().padStart(2, '0');
const year = date.getUTCFullYear();
const month = pad(date.getUTCMonth() + 1); // getUTCMonth() is 0-based
const day = pad(date.getUTCDate());
const hours = pad(date.getUTCHours());
const minutes = pad(date.getUTCMinutes());
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
const now = new Date();
console.log(getCustomUtcTimestamp(now));
Example of Output:
2025-10-18 17:47
Conclusion
Getting a UTC/GMT-based time in JavaScript is straightforward if you use the correct methods for your goal.
- To get a numerical timestamp, the best practice is to use
Date.now(). - To get a machine-readable UTC string, the recommended method is
date.toISOString(). - To get a human-readable UTC string, use
date.toUTCString(). - For custom formatting, use the specific
getUTC*methods.
By consistently using UTC for data storage and transmission, you can avoid a wide range of time zone-related bugs.