How to Get a UTC/GMT timestamp using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# Get a UTC/GMT timestamp using JavaScript

Use the getTime() method to get a UTC/GMT timestamp, e.g. new Date().getTime().

The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

index.js
// โœ… get UTC/GMT timestamp const utcTimestamp = new Date().getTime(); console.log(utcTimestamp); // ๐Ÿ‘‰๏ธ 16422369.... // โœ… get UTC/GMT date and time const gmtDateTime = new Date().toUTCString(); console.log(gmtDateTime); // ๐Ÿ‘‰๏ธ "Tue, 25 Jul 2023 17:21:46 GMT"

get utc gmt timestamp

The code for this article is available on GitHub

We used the getTime() method to get a UTC/GMT timestamp.

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

If a user is visiting your site from one time zone, the getTime() method would return the same result as for any other time zone.

The getTime() method can be used to assign a date and time to another Date object.

index.js
const utcTimestamp = new Date().getTime(); console.log(utcTimestamp); // ๐Ÿ‘‰๏ธ 16422369.... const copy = new Date(); copy.setTime(utcTimestamp); console.log(utcTimestamp === copy.getTime()); // ๐Ÿ‘‰๏ธ true

If you need the UTC/GMT representation of a date, use the toUTCString() method.

index.js
const utcDate = new Date().toUTCString(); console.log(utcDate); // ๐Ÿ‘‰๏ธ "Sat, 15 Jan 2022 09:02:48 GMT"

The toUTCString() method converts a date to a string, according to UTC/GMT.

# GMT and UTC share the same current time

Note that GMT and UTC share the same current time.

The difference between them is that GMT is a time zone, whereas UTC is a time standard and is the basis for time zones worldwide.

UTC and GMT don't change for Daylight Saving Time (DST) and always share the same current time.

If you need any of the date and time components in UTC/GMT, use the available getUTC* methods.

They are quite useful and enable us to format the date and time in many different ways, using string concatenation.
index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ returns UTC/GMT Hour of the date console.log(date.getUTCHours()); // ๐Ÿ‘‰๏ธ 17 // ๐Ÿ‘‡๏ธ returns UTC/GMT Minutes of the date console.log(date.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 23 // ๐Ÿ‘‡๏ธ returns UTC/GMT Seconds of the date console.log(date.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 8 // ๐Ÿ‘‡๏ธ returns UTC/GMT year of the date console.log(date.getUTCFullYear()); // ๐Ÿ‘‰๏ธ 2023 // ๐Ÿ‘‡๏ธ returns UTC month (0-11) // 0 is January, 11 is December console.log(date.getUTCMonth()); // ๐Ÿ‘‰๏ธ 6 // ๐Ÿ‘‡๏ธ returns UTC/GMT day of the month (1-31) console.log(date.getUTCDate()); // ๐Ÿ‘‰๏ธ 25

using different get utc methods

All of the getUTC* methods return the date or time component according to universal time.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function getGMTTime(date = new Date()) { return [ padTo2Digits(date.getUTCHours()), padTo2Digits(date.getUTCMinutes()), padTo2Digits(date.getUTCSeconds()), ].join(':'); } console.log(getGMTTime()); // ๐Ÿ‘‰๏ธ๏ธ "12:25:14"

We created a reusable function that returns the time in GMT, formatted as hh:mm:ss.

The getUTCHours() method returns the hour (0 - 23) for the specified date, according to universal time (= GMT).

The getUTCMinutes() method returns the minutes (0 - 59) for the date, according to universal time (= GMT).

The getUTCSeconds() method returns the seconds (0 - 59) for the date, according to universal time (= GMT).

In the function, we made sure to display the hours, seconds and minutes as 2 digits, even when they are less than 10.

By default, if any of the values is less than 10, the methods return a single digit, which is not what we want.

We padded the results if necessary and joined them with a colon separator.

You can adjust this depending on your use case, e.g. include the year, month, day of the month values in the formatted string.

Note that the getUTCMonth() method returns the month of the specified date as a zero-based value (0 = January, 1 = February, etc.)

You can use these values to format the UTC/GMT date in a way that suits your use case.

If you need a complete list of the getUTC* methods, visit the MDN docs.

There is a non-UTC equivalent for each of these methods, for example getUTCFullYear(0) vs Date.getFullYear().

The getUTC* methods return the date or time component according to universal time, whereas the get* methods return them according to local time (the time zone the visitor's computer is in).

The get* methods return different results depending on where the user visits your site from.

For example, if you store a local time of midnight (00:00) in your database, you wouldn't know if that's midnight in Tokyo (Japan), Paris (France), in New York (US), etc. These are all different moments that are hours apart.

For consistency, you should mostly use local time when you have to render a date and time to the user, but store the actual values in UTC.

# 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