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:
Date.UTC(year)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An 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
.
Date.UTC(year, monthIndex)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An 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
.
Date.UTC(year, monthIndex, day)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An 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
.
Date.UTC(year, monthIndex, day, hour)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month (1 through 31). Defaults to 1 if not specified. |
hour | optional | An 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
.
Date.UTC(year, monthIndex, day, hour, minute)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month (1 through 31). Defaults to 1 if not specified. |
hour | optional | An integer value representing the hour (0 through 23). Defaults to 0 if not specified. |
minute | optional | An 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
.
Date.UTC(year, monthIndex, day, hour, minute, second)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month (1 through 31). Defaults to 1 if not specified. |
hour | optional | An integer value representing the hour (0 through 23). Defaults to 0 if not specified. |
minute | optional | An integer value representing the minutes (0 through 59). Defaults to 0 if not specified. |
second | optional | An 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
.
Date.UTC(year, monthIndex, day, hour, minute, second, millisecond)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
year | required | An integer value representing the year. Must be at least 1970. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month (1 through 31). Defaults to 1 if not specified. |
hour | optional | An integer value representing the hour (0 through 23). Defaults to 0 if not specified. |
minute | optional | An integer value representing the minutes (0 through 59). Defaults to 0 if not specified. |
second | optional | An integer value representing the seconds (0 through 59). Defaults to 0 if not specified. |
millisecond | optional | An 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,
- We call the
Date.UTC()
method with the year 2021 and month index 0 (January). - The resulting timestamp is stored in the variable
timestamp
. - We log
timestamp
to the console usingconsole.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,
- We call the
Date.UTC()
method with the year 2021, month index 0 (January), and day 15. - The resulting timestamp is stored in the variable
timestamp
. - We log
timestamp
to the console usingconsole.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,
- We call the
Date.UTC()
method with the year 2021, month index 0 (January), day 15, hour 10, minute 30, and second 0. - The resulting timestamp is stored in the variable
timestamp
. - We log
timestamp
to the console usingconsole.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.