Convert a Date to GMT using JavaScript

avatar

Borislav Hadzhiev

Fri Jan 14 20222 min read

banner

Photo by Iswanto Arif

Convert an Error Object to a String in JavaScript #

Use the toUTCString() method to convert a date to 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 date = new Date(); // โœ… Get a String representing the given Date using UTC (= GMT) time zone. const result = date.toUTCString(); console.log(result); // ๐Ÿ‘‰๏ธ "Fri, 14 Jan 2022 17:30:20 GMT" // ๐Ÿ‘‡๏ธ 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()); // ๐Ÿ‘‰๏ธ 14 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Hour of the date console.log(date.getUTCHours()); // ๐Ÿ‘‰๏ธ 17 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Minutes of the date console.log(date.getUTCMinutes()); // ๐Ÿ‘‰๏ธ 30 // ๐Ÿ‘‡๏ธ returns UTC (=GMT) Seconds of the date console.log(date.getUTCSeconds()); // ๐Ÿ‘‰๏ธ 20
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.

We used the toUTCString method to convert the date to a string that uses the GMT time zone.

index.js
const date = new Date(); // โœ… Get a String representing the given Date using UTC (= GMT) time zone. const result = date.toUTCString(); console.log(result); // ๐Ÿ‘‰๏ธ "Fri, 14 Jan 2022 17:30:20 GMT"

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

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(); console.log( [ padTo2Digits(date.getUTCHours()), padTo2Digits(date.getUTCMinutes()), padTo2Digits(date.getUTCSeconds()) ].join(':') ); // ๐Ÿ‘‰๏ธ "17:30:20" function padTo2Digits(num) { return num.toString().padStart(2, '0'); }

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

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