JavaScript Date toUTCString()

Posted by

Method

Introduction to JavaScript Date toUTCString() Method

The JavaScript Date toUTCString() method is a built-in method that converts a given date object to a string in the Universal Coordinated Time (UTC) format. It is an in-built method of the Date object in JavaScript. This method returns a string representation of the date in the UTC time zone.

Syntax of the JavaScript Date toUTCString() Method

The syntax of the JavaScript Date toUTCString() method is given below:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Date.toUTCString()

[/dm_code_snippet]

Parameters Used in the JavaScript Date toUTCString() Method

The JavaScript Date toUTCString() method does not take any parameters.

Return Value of the JavaScript Date toUTCString() Method

The JavaScript Date toUTCString() method returns a string representation of the date in the UTC time zone.

Examples of the JavaScript Date toUTCString() Method

Let’s see some examples of the JavaScript Date toUTCString() method to understand it better.

Example 1: Basic Example of the JavaScript Date toUTCString() Method

In this example, we will use the JavaScript Date toUTCString() method to convert a Date object to a string in the UTC time zone.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// creating a new Date object
var d = new Date();

// displaying the string representation of the date in the UTC timezone
console.log(d.toUTCString());

[/dm_code_snippet]

Output:

Wed, 21 Oct 2020 09:19:17 GMT

Example 2: Example of the JavaScript Date toUTCString() Method with a Custom Date

In this example, we will use the JavaScript Date toUTCString() method to convert a custom Date object to a string in the UTC time zone.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// creating a new Date object
var d = new Date(2021, 8, 15);

// displaying the string representation of the date in the UTC timezone
console.log(d.toUTCString());

[/dm_code_snippet]

Output:

Mon, 13 Sep 2021 00:00:00 GMT