Get the current Date and Time in UTC/GMT using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
5 min

banner

# Table of Contents

  1. Get the current Date and Time in UTC/GMT using JavaScript
  2. Get a GMT/UTC Date or convert a Date to GMT/UTC in JavaScript

# Get the current Date and Time in UTC/GMT using JavaScript

Use the toUTCString() method to get the current date and time in UTC/GMT, e.g. new Date().toUTCString().

The method converts the date to a string, using the UTC time zone, which shares the same current time with GMT.

index.js
const utcStr = new Date().toUTCString(); console.log(utcStr); // ๐Ÿ‘‰๏ธ "Mon, 24 Jul 2023 16:46:35 GMT" const isoStr = new Date().toISOString(); console.log(isoStr); // ๐Ÿ‘‰๏ธ "2023-07-24T16:46:35.269Z"

get current date and time in utc gmt

The code for this article is available on GitHub

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

# Get the current Date and Time in UTC/GMT using toISOString()

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); // ๐Ÿ‘‰๏ธ "2023-07-24T16:47:57.673Z"

get current date and time in utc gmt using toisostring

The code for this article is available on GitHub

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 dates and times 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
The code for this article is available on GitHub

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.

# Get a GMT/UTC Date or convert a Date to GMT/UTC in JavaScript

You can also use the toUTCString() method to get a UTC/GMT date set to a different time.

The method converts the date to a string, using the UTC time zone, which shares the same current time with GMT.

index.js
// โœ… (Optionally) Create a Date using Universal time (= GMT) // instead of local time const d1 = new Date(Date.UTC(2022, 0, 14, 14, 30, 0)); console.log(d1); // -------------------------------------------- // โœ… Get a String representing the given Date // using UTC (= GMT) time zone. const d2 = new Date(); const result = d2.toUTCString(); console.log(result); // ๐Ÿ‘‰๏ธ "Fri, 14 Jan 2022 16:50:03 GMT" // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Hour of the date console.log(d2.getUTCHours()); // ๐Ÿ‘‰๏ธ 16 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Minutes of the date console.log(d2.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 50 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Seconds of the date console.log(d2.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 03
The code for this article is available on GitHub
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.

In the first example, we used the Date.UTC method, which treats the passed-in parameters as UTC.

index.js
const d1 = new Date(Date.UTC(2022, 0, 14, 14, 30, 0)); console.log(d1); // ๐Ÿ‘‰๏ธ 2022-01-14T14:30:00.000Z
We used this approach to create a date object that has its time set according to GMT and not local time (the visitor's computer time zone).

The parameters we passed to the Date.UTC method are the year, month (0 - 11), day, hour, minute, second.

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 get the GMT/UTC representation of the date.

index.js
const d2 = new Date(); const result = d2.toUTCString(); console.log(result); // ๐Ÿ‘‰๏ธ "Fri, 14 Jan 2022 16:50:03 GMT"

The method converts the date to a string, using the UTC time zone, which shares the same current time with GMT.

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

index.js
const d2 = new Date(); const result = d2.toUTCString(); console.log(result); // ๐Ÿ‘‰๏ธ "Fri, 14 Jan 2022 16:50:03 GMT" // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Hour of the date console.log(d2.getUTCHours()); // ๐Ÿ‘‰๏ธ 16 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Minutes of the date console.log(d2.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 50 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Seconds of the date console.log(d2.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 03 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) year of the date console.log(d2.getUTCFullYear()); // ๐Ÿ‘‰๏ธ 2022 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) month (0-11) // 0 is January, 11 is December console.log(d2.getUTCMonth()); // ๐Ÿ‘‰๏ธ 0 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) day of the month (1-31) console.log(d2.getUTCDate()); // ๐Ÿ‘‰๏ธ 14

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

You can use these values to format the GMT/UTC date or time in a way that suits your use case.
index.js
const date = new Date(); console.log( [ padTo2Digits(date.getUTCHours()), padTo2Digits(date.getUTCMinutes()), padTo2Digits(date.getUTCSeconds()) ].join(':') ); // ๐Ÿ‘‰๏ธ "17:30:20" (hh:mm:ss) function padTo2Digits(num) { return num.toString().padStart(2, '0'); }
The code for this article is available on GitHub

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

You can view all of the getUTC methods by visiting the MDN docs.

There is a non-UTC equivalent for each of these methods, for example getUTCFullYear() vs Date.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), 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).

I've also written an article on how to get a UTC/GMT timestamp in JS.

# 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