Get the current Date and Time in UTC using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 15, 2022

banner

Photo from Unsplash

Get the current Date and Time in UTC using JavaScript #

Use the toUTCString() method to get the current date and time in utc, e.g. new Date().toUTCString(). The toUTCString method converts a date to a string using the UTC time zone.

index.js
const utcStr = new Date().toUTCString(); console.log(utcStr); // 👉️ "Sat, 15 Jan 2022 16:17:30 GMT" const isoStr = new Date().toISOString(); console.log(isoStr); // 👉️ "2022-01-15T16:17:30.814Z"

The toUTCString method returns a string that represents the date using the UTC time zone.

You could also use the toISOString() method to get a string in the ISO 8601 format of YYYY-MM-DDTHH:mm:ss.sssZ.

The Z at the end of the format means UTC, that is, an offset from UTC of zero hours, minutes and seconds.

index.js
const isoStr = new Date().toISOString(); console.log(isoStr); // 👉️ "2022-01-15T16:17:30.814Z"

The toISOString method returns a string representing the date in the ISO 8601 format, according to universal time.

ISO 8601 formatted strings are commonly used to store date and time on the server.

You can also use the available getUTC* methods that return the date and time components according to universal time.

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(); // 👉️ "Sat Jan 15 2022 18:23:51 GMT+0200" console.log(date); // 👇️ 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 // 👇️ returns UTC Hour of the date console.log(date.getUTCHours()); // 👉️ 16 // 👇️ returns UTC Minutes of the date console.log(date.getUTCMinutes()); // 👉️ 23 // 👇️ returns UTC Seconds of the date console.log(date.getUTCSeconds()); // 👉️ 51

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

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

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

Here is an example that uses the getUTC* methods to format a Date as YYYY-MM-DD hh:mm:ss.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ date.getFullYear(), padTo2Digits(date.getUTCMonth() + 1), padTo2Digits(date.getUTCDate()), ].join('-') + ' ' + [ padTo2Digits(date.getUTCHours()), padTo2Digits(date.getUTCMinutes()), padTo2Digits(date.getUTCSeconds()), ].join(':') ); } // 👇️ "2022-01-15 16:25:12" console.log(formatDate(new Date()));

We created a reusable function that formats the date and time as YYYY-MM-DD hh:mm:ss using the UTC time standard.

You could reorder the date components, change the separator to a forward slash /, or make any other changes that suit your use case.

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.

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.