Open In App

JavaScript Date UTC() Method

In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Date UTC() Method Syntax:

Date.UTC(year, month, day, hour, minute, second, millisecond);

Date UTC() Method Parameters:

Date UTC() Method Return value:

The Date.UTC() method returns a number representing the number of milliseconds in the given Date object since January 1, 1970, 00:00:00, universal time.



Date UTC() Method Example:

Below is an example of the Date UTC() method.




let gfg = Date.UTC(2020, 07, 03);
console.log("Output : " + gfg);

Output

Output : 1596412800000

Explanation:

The code creates a UTC date object representing August 3, 2020, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The value is then logged to the console.

Date UTC() Method Example:

Three parameters are passed in the Date.UTC() method represents the year, month, and day respectively. The method returns the number of milliseconds between the date specified as the parameter and midnight of January 1, 1970.




let test = Date.UTC(2010, 01, 28);
console.log("Output : " + test);

Output
Output : 1267315200000

Explanation:

The code creates a UTC date object representing February 28, 2010, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. The value is then logged to the console.

JavaScript Date UTC() Method – UseCases:

1. JavaScript Get the start and end of the day in UTC

To get the start and end of the day in UTC, create a new Date object for the desired day, set the hours, minutes, seconds, and milliseconds to 0 for the start of the day, and set hours to 23, minutes to 59, seconds to 59, and milliseconds to 999 for the end of the day.

2. How to convert a JavaScript date to UTC?

To convert a JavaScript date to UTC, use the `toUTCString()`, `toISOString()`, or `toUTCString()` methods. Alternatively, use methods like `getUTCFullYear()`, `getUTCMonth()`, and `getUTCDate()` to construct a UTC date object.

Supported Browsers

We have a complete list of Javascript Date Objects, to check those please go through the Javascript Date Object Complete reference article.


Article Tags :