JavaScript Date setDate() Method

JavaScript Date setDate() method is “used to set the date of a month into a date object created using the date() constructor.”

Syntax

dateObj.setDate(date_Value);

Parameters

date_value: It returns the new, i.e, updated, date of the month, which is set by the setDate() method. 

Return value

The setDate() method returns the number of milliseconds between the date object and midnight of January 1, 1970.

Example 1: Setting the Day of the Month

This example sets the day of the month to the 15th.

let date = new Date();
console.log("Original Date: " + date);

date.setDate(15);
console.log("Updated Date: " + date);

Output

Original Date: Wed Aug 16 2023 10:33:44 GMT+0530 (India Standard Time)

Updated Date: Tue Aug 15 2023 10:33:44 GMT+0530 (India Standard Time)

Example 2: Setting a Past Day

If you set a day of the month that is less than 1, the date will be set to the last day of the previous month. This example sets the date to the last day of the previous month.

let date = new Date();
console.log("Original Date: " + date);

date.setDate(0);
console.log("Updated Date (Last day of previous month): " + date);

Output

Original Date: Wed Aug 16 2023 10:38:38 GMT+0530 (India Standard Time)

Updated Date (Last day of previous month): Mon Jul 31 2023 10:38:38 
              GMT+0530 (India Standard Time)

Example 3: Setting a Future Day

If you set a day of the month that exceeds the number of days, the date will roll over to the next month. This example sets the date to the 5th day of the next month.

let date = new Date();
console.log("Original Date: " + date);

let daysInCurrentMonth = new Date(date.getFullYear(), 
                          date.getMonth() + 1, 0).getDate();

date.setDate(daysInCurrentMonth + 5);
console.log("Updated Date (5th day of next month): " + date);

Output

Original Date: Wed Aug 16 2023 10:41:09 GMT+0530 (India Standard Time)

Updated Date (5th day of next month): Tue Sep 05 2023 
              10:41:09 GMT+0530 (India Standard Time)

Example 4: Do not give any date of the month

If you don’t supply any parameters to the Date() constructor, it initializes the date object to the current date and time. Once you have that date object, you can use the setDate() method to change the day of the month.

let currentDate = new Date();
console.log("Current Date: " + currentDate);

currentDate.setDate(20);
console.log("Updated Date: " + currentDate);

Output

Current Date: Wed Aug 16 2023 10:43:36 GMT+0530 (India Standard Time)

Updated Date: Sun Aug 20 2023 10:43:36 GMT+0530 (India Standard Time)

Example 5: If nothing as a parameter is given in the Date() constructor

When you create a Date object in JavaScript without any parameters, it defaults to the current date and time. The setDate() method only modifies the day of the month, not the month or year.

If you change the month’s day to a value beyond the current month’s number of days, the date will roll over to the next month.

let currentDate = new Date(); // This is August 10, 2023
console.log("Current Date: " + currentDate);

currentDate.setDate(25); // This will set the date to August 25, 2023
console.log("Updated Date: " + currentDate);

Output

If nothing as a parameter is given in the Date() constructor

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript Intl.DateTimeFormat.format()

JavaScript Date getDay()

Convert Seconds to Minutes and Seconds in JavaScript

Leave a Comment