What is toUTCString() in JavaScript?
toUTCString() is a JavaScript date object method that converts a date to a string using the
Syntax
dateObj.toUTCString()
Parameters
No parameters are passed to this method. toUTCString() only converts the contents of a Date() constructor object.
Return value
toUTCString() returns a string converted according to UTC. The value is in the form of Www, dd Mmm yyyy hh:mm:ss GMT.
| Format | Description |
|---|---|
| Www | Day of the week |
| dd | Day of the month |
| Mmm | Month |
| yyyy | Year |
| hh | Hour |
| mm | Minute |
| ss | Seconds |
Example
In the example below, we will take a look at Date constructor objects with their contents and see how toUTCSTring() converts them.
// Data objects with their contentslet dateobj1 = new Date();let dateobj2 = new Date('October 15, 1996 05:35:32');let dateobj3 = new Date('4, 5, 6');let dateobj4 = new Date("2021-10-25");let dateobj5 = new Date("2021/10/25");// Printing UTC converted Date stringsconsole.log(dateobj1.toUTCString())console.log(dateobj2.toUTCString())console.log(dateobj3.toUTCString())console.log(dateobj4.toUTCString())console.log(dateobj5.toUTCString())
Note: The
toUTCString()method returns invalid date if the initial content of theDateobject is incorrect.
See the example below:
// Data objects with incorrect date stringslet dateobj1 = new Date('7, 8, 3, 4');let dateobj2 = new Date("2021-10-2021");let dateobj3 = new Date("2021/10/2021");console.log(dateobj1.toUTCString())console.log(dateobj2.toUTCString())console.log(dateobj3.toUTCString())