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

ParameterOptional/RequiredDescription

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,

  1. We create a new Date object date representing January 15, 2021.
  2. We use the setUTCDate() method to set the day of the month to 20.
  3. The modified date is stored in the date object.
  4. We log the modified date to the console using console.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,

  1. We create a new Date object now representing the current date and time.
  2. We use the setUTCDate() method to set the day of the month to 15.
  3. The modified date is stored in the now object.
  4. We log the modified now to the console using console.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,

  1. We create a new Date object pastDate representing July 4, 1776.
  2. We use the setUTCDate() method to set the day of the month to 10.
  3. The modified date is stored in the pastDate object.
  4. We log the modified pastDate to the console using console.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.