Last updated: Mar 5, 2024
Reading timeยท5 min
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.
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"
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.
const isoStr = new Date().toISOString(); console.log(isoStr); // ๐๏ธ "2023-07-24T16:47:57.673Z"
The toISOString
method returns a string representing the date in the ISO 8601
format, according to universal time.
You can also use the available getUTC*
methods that return the date and time
components according to universal time.
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.
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
.
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.
/
, or make any other changes that suit your use case.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.
// โ (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 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.
const d1 = new Date(Date.UTC(2022, 0, 14, 14, 30, 0)); console.log(d1); // ๐๏ธ 2022-01-14T14:30:00.000Z
The parameters we passed to the Date.UTC
method are the year
, month
(0 -
11), day
, hour
, minute
, second
.
In the second example, we used the toUTCString method to get the GMT/UTC representation of the date.
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).
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).
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'); }
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 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.
You can learn more about the related topics by checking out the following tutorials: