JavaScript Date setMonth() Method is a built-in function in JavaScript’s Date object, which is used to set the numeric value of the month for a given date object.
dateObj.setMonth(month, day)
It takes one or two arguments – the first one being the month value, and the second one (optional) being the day of the month. If the day of the month is not specified, the method sets the day to the first day of the new month by default.
JavaScript Date setMonth() Method example
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
const date = new Date('2022-01-01');
console.log(date);
// Set the month to February
date.setMonth(1);
console.log(date);
// Set the month to December and day to 15
date.setMonth(11, 15);
console.log(date);
</script>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS Date method topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version