JavaScript Date.UTC()
Syntax & Examples


Date.UTC() static-method

The Date.UTC() method accepts parameters for year, month, day, hour, minute, second, and millisecond, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This method is useful for creating timestamps in UTC time zone.


Syntax of Date.UTC()

There are 7 variations for the syntax of Date.UTC() static-method. They are:

1.
Date.UTC(year)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year.

Returns value of type Number.

2.
Date.UTC(year, monthIndex)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year and month.

Returns value of type Number.

3.
Date.UTC(year, monthIndex, day)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month (1 through 31). Defaults to 1 if not specified.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year, month, and day.

Returns value of type Number.

4.
Date.UTC(year, monthIndex, day, hour)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month (1 through 31). Defaults to 1 if not specified.
houroptionalAn integer value representing the hour (0 through 23). Defaults to 0 if not specified.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year, month, day, and hour.

Returns value of type Number.

5.
Date.UTC(year, monthIndex, day, hour, minute)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month (1 through 31). Defaults to 1 if not specified.
houroptionalAn integer value representing the hour (0 through 23). Defaults to 0 if not specified.
minuteoptionalAn integer value representing the minutes (0 through 59). Defaults to 0 if not specified.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year, month, day, hour, and minute.

Returns value of type Number.

6.
Date.UTC(year, monthIndex, day, hour, minute, second)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month (1 through 31). Defaults to 1 if not specified.
houroptionalAn integer value representing the hour (0 through 23). Defaults to 0 if not specified.
minuteoptionalAn integer value representing the minutes (0 through 59). Defaults to 0 if not specified.
secondoptionalAn integer value representing the seconds (0 through 59). Defaults to 0 if not specified.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year, month, day, hour, minute, and second.

Returns value of type Number.

7.
Date.UTC(year, monthIndex, day, hour, minute, second, millisecond)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1970.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month (1 through 31). Defaults to 1 if not specified.
houroptionalAn integer value representing the hour (0 through 23). Defaults to 0 if not specified.
minuteoptionalAn integer value representing the minutes (0 through 59). Defaults to 0 if not specified.
secondoptionalAn integer value representing the seconds (0 through 59). Defaults to 0 if not specified.
millisecondoptionalAn integer value representing the milliseconds (0 through 999). Defaults to 0 if not specified.

This static-method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the specified year, month, day, hour, minute, second, and millisecond.

Returns value of type Number.



✐ Examples

1 Using Date.UTC() with year and month

In JavaScript, we can use the Date.UTC() method to get the timestamp for a specific year and month.

For example,

  1. We call the Date.UTC() method with the year 2021 and month index 0 (January).
  2. The resulting timestamp is stored in the variable timestamp.
  3. We log timestamp to the console using console.log() method.

JavaScript Program

const timestamp = Date.UTC(2021, 0);
console.log(timestamp);

Output

1609459200000

2 Using Date.UTC() with year, month, and day

In JavaScript, we can use the Date.UTC() method to get the timestamp for a specific year, month, and day.

For example,

  1. We call the Date.UTC() method with the year 2021, month index 0 (January), and day 15.
  2. The resulting timestamp is stored in the variable timestamp.
  3. We log timestamp to the console using console.log() method.

JavaScript Program

const timestamp = Date.UTC(2021, 0, 15);
console.log(timestamp);

Output

1610668800000

3 Using Date.UTC() with full date and time

In JavaScript, we can use the Date.UTC() method to get the timestamp for a specific date and time.

For example,

  1. We call the Date.UTC() method with the year 2021, month index 0 (January), day 15, hour 10, minute 30, and second 0.
  2. The resulting timestamp is stored in the variable timestamp.
  3. We log timestamp to the console using console.log() method.

JavaScript Program

const timestamp = Date.UTC(2021, 0, 15, 10, 30, 0);
console.log(timestamp);

Output

1610700600000

Summary

In this JavaScript tutorial, we learned about UTC() static-method of Date: the syntax and few working examples with output and detailed explanation for each example.