JavaScript Date.toUTCString() Method
-
Syntax of JavaScript
date.toUTCString()
: -
Example Code: Use
date.toUTCString()
to Change a Date Object Into UTC Time Zone -
Example Code: Use
date.toUTCString()
to Change a Specific Date and Time Into the Universal Time Format
The date.toUTCString()
method converts the date object into a string according to the Universal Coordinated Time. This method uses the same format to change the date and time as .toGMTString()
.
Syntax of JavaScript date.toUTCString()
:
let date = new Date();
date.toUTCString();
Parameter
This method does not take any parameters.
Return
This method takes a date object and returns a string representing universal time.
Example Code: Use date.toUTCString()
to Change a Date Object Into UTC Time Zone
There is no need to add a parameter to convert a date object to a string using the date.toUTCString()
method.
In this example, we created a date object to get the current date and time using the new Date()
method. We then used the date.toUTCString()
method to get the current date and time in the UTC zone.
const date = new Date();
let utc = date.toUTCString();
console.log(utc);
Output:
Thu, 18 Aug 2022 14:45:10 GMT
Example Code: Use date.toUTCString()
to Change a Specific Date and Time Into the Universal Time Format
We use the date.toUTCString()
method in JavaScript to get the date and time according to UTC.
In this example, we have created a date object with a date and time. The .toUTCString()
method took the date object and converted it into a string.
const d = new Date('2022, 4, 18, 1:20:05');
let utc = d.toUTCString();
console.log(utc);
Output:
Mon, 18 Apr 2022 01:20:05 GMT
The date.toUTCString()
method works on all the current versions of browsers.