JavaScript Date setUTCDate()
Syntax & Examples
setUTCDate() method
The setUTCDate() method of the Date object in JavaScript sets the day of the month for a specified date according to universal time.
Syntax of setUTCDate()
The syntax of Date.setUTCDate() method is:
setUTCDate(dateValue)This setUTCDate() method of Date sets the day of the month for a specified date according to universal time.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
|
Return Type
Date.setUTCDate() returns value of type Number.
✐ Examples
1 Using setUTCDate() to set the day of the month
In JavaScript, we can use the setUTCDate() method to set the day of the month for a Date object according to universal time.
For example,
- We create a new Date object
daterepresenting January 15, 2021. - We use the
setUTCDate()method to set the day of the month to 20. - The modified date is stored in the
dateobject. - We log the modified
dateto the console usingconsole.log()method.
JavaScript Program
const date = new Date('2021-01-15T00:00:00Z');
date.setUTCDate(20);
console.log(date);Output
2021-01-20T00:00:00.000Z
2 Using setUTCDate() with the current date
In JavaScript, we can use the setUTCDate() method to set the day of the month for the current date according to universal time.
For example,
- We create a new Date object
nowrepresenting the current date and time. - We use the
setUTCDate()method to set the day of the month to 15. - The modified date is stored in the
nowobject. - We log the modified
nowto the console usingconsole.log()method.
JavaScript Program
const now = new Date();
now.setUTCDate(15);
console.log(now);Output
The output will vary based on the current date and time, with the day of the month set to 15.
3 Using setUTCDate() to set the day of the month in the past
In JavaScript, we can use the setUTCDate() method to set the day of the month for a date in the past according to universal time.
For example,
- We create a new Date object
pastDaterepresenting July 4, 1776. - We use the
setUTCDate()method to set the day of the month to 10. - The modified date is stored in the
pastDateobject. - We log the modified
pastDateto the console usingconsole.log()method.
JavaScript Program
const pastDate = new Date('1776-07-04T00:00:00Z');
pastDate.setUTCDate(10);
console.log(pastDate);Output
1776-07-10T00:00:00.000Z
Summary
In this JavaScript tutorial, we learned about setUTCDate() method of Date: the syntax and few working examples with output and detailed explanation for each example.