JavaScript Date toUTCString()
Syntax & Examples
Date.toUTCString() method
The toUTCString() method of the Date object in JavaScript converts a date to a string using the UTC timezone. The string format is a human-readable representation of the date and time in the UTC timezone.
Syntax of Date.toUTCString()
The syntax of Date.toUTCString() method is:
toUTCString()
This toUTCString() method of Date converts a date to a string using the UTC timezone.
Return Type
Date.toUTCString() returns value of type String
.
✐ Examples
1 Using toUTCString() to get the UTC string of a Date object
In JavaScript, we can use the toUTCString()
method to get the UTC string representation of a Date object.
For example,
- We create a new Date object
date
representing January 1, 2000. - We use the
toUTCString()
method to get the UTC string of thedate
object. - The UTC string is stored in the variable
utcString
. - We log
utcString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('2000-01-01T00:00:00Z');
const utcString = date.toUTCString();
console.log(utcString);
Output
Sat, 01 Jan 2000 00:00:00 GMT
2 Using toUTCString() with a specific date
We can use the toUTCString()
method to get the UTC string of a specific Date object.
For example,
- We create a new Date object
date
representing July 4, 1776. - We use the
toUTCString()
method to get the UTC string of thedate
object. - The UTC string is stored in the variable
utcString
. - We log
utcString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('1776-07-04T00:00:00Z');
const utcString = date.toUTCString();
console.log(utcString);
Output
Thu, 04 Jul 1776 00:00:00 GMT
3 Using toUTCString() with the current date
We can use the toUTCString()
method to get the UTC string representation of the current date.
For example,
- We create a new Date object
date
representing the current date and time. - We use the
toUTCString()
method to get the UTC string of thedate
object. - The UTC string is stored in the variable
utcString
. - We log
utcString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date();
const utcString = date.toUTCString();
console.log(utcString);
Output
Depends on the current date and time (e.g., 'Mon, 27 May 2024 12:34:56 GMT')
Summary
In this JavaScript tutorial, we learned about toUTCString() method of Date: the syntax and few working examples with output and detailed explanation for each example.