Skip to content
Advertisements

JavaScript Date.setMonth()

Why setMonth ?

JavaScript Date The setMonth () method is a built-in function. This sets the month value for a specific date.

  • january 0
  • february 1
  • mart 2
  • april 3
  • may 4
  • june 5
  • july 6
  • august 7
  • september 8
  • October 9
  • november 10
  • december 11

Syntax:

DateObj.setMonth( month );

  • month : We’re entering the month we want to assign. The value of the month is between 0 and 11. You can understand better by looking at the article above.

Return:

  • It returns the new i.e updated month which is set by setMonth() function.

JavaScript setMonth Examples

Example 1

You can change the month of a specific date with the setMonth () method.

        var date = new Date('April 21, 1983 22:47:55');
        console.log(date.getMonth());

        date.setMonth(2);
        console.log(date.getMonth())

3
2


Example 2

You can change the month of the current date with the setMonth () method.

        var date = new Date();
        console.log(date.getMonth());

        date.setMonth(11);
        console.log(date.getMonth())

1
11


Example 3

If you do not specify the DayValue parameter, the value returned from the getDate method is used. If a parameter that you specify is outside the expected range, setMonth tries to update the date information in the Date object accordingly. For example, if you use 18 for the monthValue, the year will increase by 1 (year + 1) and will be used for 6 months.

        var date = new Date('April 21, 1983 22:47:55');
        console.log(date.getMonth());

        date.setMonth(18);
        console.log(date.getMonth())

3
6


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: