Get a GMT Date using JavaScript

avatar

Borislav Hadzhiev

Fri Jan 14 20222 min read

banner

Photo by Al Elmes

Get a GMT Date using JavaScript #

Use the toUTCString() method to get the GMT representation of a date, 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
// โœ… (Optionally) Create 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
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.

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 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 returns a string that represents the given date using the GMT time zone.

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

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