JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Date Right Arrow setMonth()

setMonth()  Date  instance method

Description

Sets the locale specific month of the year (0-11) for the specified date.

Syntax


Signature Description
aDate.setMonth(monthInt[, dayInt])Sets the locale specific month of the year (0-11) for the specified date.

Parameters


Parameter Description
monthIntAn integer in the range 0-11. January=0, February=1 etc.
dayIntAn integer in the range 1-31.

  • If dayInt is not specified the value returned from the getDate() method is used.

For specified values outside the ranges above the setMonth() method will try and set the date accordingly.

Examples

The code below shows examples of the setMonth() method of Date.


// Create a Date instance for the current date and time.
var todaysDate = new Date();
alert(todaysDate);

// Set month.
todaysDate.setMonth(3);
alert(todaysDate + ' - Month set to 3');

// Set month and day.
todaysDate.setMonth(7,7);
alert(todaysDate + ' - Month and day set');


Press the button below to action the above code:


Related Tutorials

JavaScript Intermediate Tutorials - Lesson 2 - Dates and Times



  

go to home page Homepage go to top of page Top