Get a GMT timestamp using JavaScript

avatar

Borislav Hadzhiev

Sat Jan 15 20222 min read

Get a GMT timestamp using JavaScript #

Use the getTime() method to get a 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. UTC shares the same current time with GMT.

index.js
// โœ… get GMT timestamp const gmtTimestamp = new Date().getTime(); console.log(gmtTimestamp); // ๐Ÿ‘‰๏ธ 1642232.... // โœ… get GMT date and time const gmtDateTime = new Date().toUTCString(); console.log(gmtDateTime); // ๐Ÿ‘‰๏ธ "Sat, 15 Jan 2022 07:41:11 GMT"

We used the Date.getTime method to get a GMT timestamp.

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

The method returns the same timestamp, regardless of your visitor's time zones.

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.

In the second example, we used the toUTCString method to convert the date to a string that uses the GMT time zone.

index.js
// โœ… get GMT date and time const gmtDateTime = new Date().toUTCString(); console.log(gmtDateTime); // ๐Ÿ‘‰๏ธ "Sat, 15 Jan 2022 07:41:11 GMT"

If you need any of the date and time components in 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()); // ๐Ÿ‘‰๏ธ 7 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Minutes of the date console.log(date.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 56 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Seconds of the date console.log(date.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 46 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) year of the date console.log(date.getUTCFullYear()); // ๐Ÿ‘‰๏ธ 2022 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) month (0-11) // 0 is January, 11 is December console.log(date.getUTCMonth()); // ๐Ÿ‘‰๏ธ 0 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) day of the month (1-31) console.log(date.getUTCDate()); // ๐Ÿ‘‰๏ธ 15

All of the getUTC* methods return the date or time component according to universal time (= GMT).

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 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 vs getFullYear.

The getUTC* methods return the date or time component according to universal time (= GMT), 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), in 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 (=GMT).

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee