JavaScript Date setUTCDate() Method



The JavaScript Date.setUTCDate() method is used to set the day of the month of a Date object according to Universal Coordinated Time (UTC). It allows you to modify the day component of a Date object without affecting other components like the month, year, or time. This method does not change the time zone offset or the time components of the date object.

UTC stands for Coordinated Universal Time. It is the primary time standard by which the world regulates clocks and time. UTC is often referred to as "GMT" (Greenwich Mean Time).

Syntax

Following is the syntax of JavaScript Date setUTCDate() method −

setUTCDate(dateValue);

Parameters

This method accepts only one parameter. The same is described below −

Return value

  • dateValue An integer between 1 and 31.
    • If 0 is provided, it will result in the last day of the previous month.
    • If -1 is provided, it will result in day before the last day of the previous month.
    • If 32 is provided, it will result in the first day of the next month (if that month has 31 days).
    • If 32 is provided, it will result in the second day of the next month (if that month has 30 days).

Return Value

This method does not return a value. It modifies the day component of the Date object in place.

Example 1

In the following example, we are using the JavaScript Date setUTCDate() method to set the UTC day of the month to 15 −






Output

If we execute the above program, the day of the month will be set to 15.

Example 2

Here, setting the day of the month (according to UTC) to a specified date "May 25, 2023".






Output

If we execute the above program, the day of the month will be set to 10.

Example 3

If we provide "0" for a dateValue, this method will return the last day of the previous month −






Output

The updated timestamp will be "Sun Apr 30 2023 05:30:00 GMT+0530 (India Standard Time)".

Example 4

If we provide "-1" for a dateValue, this method will return the day before the last day of the previous month −






Output

The updated timestamp will be "Sat Apr 29 2023 05:30:00 GMT+0530 (India Standard Time)".

Example 5

If we provide "32" for a dateValue, this method will return the first day of the next month −






Output

The updated timestamp will be "Thu Jun 01 2023 05:30:00 GMT+0530 (India Standard Time)".

Advertisements