Get a UTC timestamp using JavaScript

avatar

Borislav Hadzhiev

Sat Jan 15 20223 min read

banner

Photo by Heidi Fin

Get a UTC timestamp using JavaScript #

Use the getTime() method to get a UTC 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
const utcTimestamp = new Date().getTime(); console.log(utcTimestamp); // ๐Ÿ‘‰๏ธ 16422369....

We used the Date.getTime method to get a UTC 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 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.

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, 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 Hour of the date console.log(date.getUTCHours()); // ๐Ÿ‘‰๏ธ 7 // ๐Ÿ‘‡๏ธ returns UTC Minutes of the date console.log(date.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 10 // ๐Ÿ‘‡๏ธ returns UTC Seconds of the date console.log(date.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 57 // ๐Ÿ‘‡๏ธ returns UTC year of the date console.log(date.getUTCFullYear()); // ๐Ÿ‘‰๏ธ 2022 // ๐Ÿ‘‡๏ธ returns UTC month (0-11) // 0 is January, 11 is December console.log(date.getUTCMonth()); // ๐Ÿ‘‰๏ธ 0 // ๐Ÿ‘‡๏ธ returns UTC 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.

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 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, 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.

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