The JavaScript setDate function is one of the Date Functions, which is useful for setting the day of the month of a given date as per the local time zone.
The syntax of the setDate function is as follows.
Date.setDate(Day_Number)
Parameter: The JavaScript setdate() function accepts an integer number representing the day of the month.
If we use a number outside the range of 1 and 31, it updates the date. For instance, the current date is May 1st, and if we set Day_number = 40, the setDate() changes the month to June and the date to 9th. 40 = (31 days in May + 9 days in June).
Return Value: The setDate() updates the current date using the day of the month number and returns a new timestamp.
JavaScript setDate add days example
In the following example, we use the setDate() function to set the current day (from date) to the 25th. As you can see from the output below, the date has changed from June 15, 2026, to June 25th 2026.
const dt = new Date();
console.log(dt);
dt.setDate(25);
console.log(dt);
Mon Jun 15 2026 15:11:29 GMT+0530 (India Standard Time)
Thu Jun 25 2026 15:11:29 GMT+0530 (India Standard Time)
Example 2: In this JavaScript example, we use the date() object to parse the string date and then use the setDate() function to set the day number of a custom date to 31.
const dt = new Date("December 1, 2023 10:11:22");
console.log(dt);
dt.setDate(31);
console.log(dt);
Fri Dec 01 2023 10:11:22 GMT+0530 (India Standard Time)
Sun Dec 31 2023 10:11:22 GMT+0530 (India Standard Time)
Example 3: In this code, we set the setDate() method to set the day number of the custom dates (without Day or Month) to 31. By default, the Date() object assigns January 1st as the default month and day. So, set the day of the month number from default 01 to 31.
const dt = new Date("2023");
console.log(dt);
dt.setDate(31);
console.log(dt);
Sun Jan 01 2023 05:30:00 GMT+0530 (India Standard Time)
Tue Jan 31 2023 05:30:00 GMT+0530 (India Standard Time)
Example 4: As we mentioned earlier, if we use a day of the month number above 31, the setDate() method updates the month number (moves to the next month).
const dt = new Date("May 1, 2026");
console.log(dt);
dt.setDate(40);
console.log(dt);
Fri May 01 2026 00:00:00 GMT+0530 (India Standard Time)
Tue Jun 09 2026 00:00:00 GMT+0530 (India Standard Time)
Adding 7 days to the current date in JavaScript
We can use the getDate() method to get the current day of the month number and then use setDate() to add 7 days to it.
const dt = new Date();
console.log(dt);
dt.setDate(dt.getDate() + 7);
console.log(dt);
Mon Jun 15 2026 15:20:16 GMT+0530 (India Standard Time)
Mon Jun 22 2026 15:20:16 GMT+0530 (India Standard Time)
Subtract 5 days from the current date in JavaScript
Similar to the above, we can use the getDate() and setDate() methods to subtract n number of days from the current date and time or a custom datetime.
const dt = new Date();
console.log(dt);
dt.setDate(dt.getDate() - 5);
console.log(dt);
Mon Jun 15 2026 15:25:22 GMT+0530 (India Standard Time)
Wed Jun 10 2026 15:25:22 GMT+0530 (India Standard Time)
JavaScript setDate negative numbers
We can use the setDate() method with negative day of the month numbers. If you do so, it goes to the previous month. For example, 0 means last day, setDate(-1) returns the last but one day of the last month. If we set it to -2, it returns May 29th.
const dt = new Date();
console.log(dt);
dt.setDate(-1);
console.log(dt);
Mon Jun 15 2026 15:21:35 GMT+0530 (India Standard Time)
Sat May 30 2026 15:21:35 GMT+0530 (India Standard Time)
JavaScript setDate last day of the month
As the current month is June, the getMonth() returns 5, and incrementing by 1 means the month becomes July 2026. Next, 0 mean last day of the previous month, which is June 30th, 2026.
const dt = new Date();
dt.setMonth(dt.getMonth() + 1, 0);
console.log(dt);
Tue Jun 30 2026 15:27:09 GMT+0530 (India Standard Time)