392

I want to add months to a date in JavaScript.

For example: I am inserting date 06/01/2011 (format mm/dd/yyyy) and now I want to add 8 months to this date. I want the result to be 02/01/2012.

So when adding months, the year may also increase.

10
  • If, you add 8 months to the date 06/01/2011 how it will be changed to 02/01/2011, Please can you make it some what clear Commented Apr 13, 2011 at 6:07
  • 1
    this is a hard problem to get right - what happens if you're on the 31st of a month and the target month doesn't have 31 days? Even worse, what if the target month is February?
    – Alnitak
    Commented Apr 13, 2011 at 6:08
  • @kalyan : if I add 8 months to the date 06/01/2011(mm/dd/yyyy) then it will be 02/01/2012 because if we add 06+08 = 14 and we have only 12 months so it will change the year and give the result as 02/01/2012. Commented Apr 13, 2011 at 6:12
  • @Alnitak : No dear it will only start with 1st date of month. Commented Apr 13, 2011 at 6:13
  • 1
    then you should have said so....
    – Alnitak
    Commented Apr 13, 2011 at 6:15

3 Answers 3

505

Corrected as of 25.06.2019:

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

var newDate = new Date(date.setMonth(date.getMonth() + 8));
console.log(newDate);

Old From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = new Date(jan312009.setMonth(jan312009.getMonth()+8));
16
  • 70
    that's not a typo - the Date constructor uses 0 for January, not 1.
    – Alnitak
    Commented Apr 13, 2011 at 6:19
  • 2
    @Sampson. No, it will display March 3rd 2009 :) Commented Aug 22, 2013 at 14:10
  • 10
    Check this solution for last days in month ;)
    – Szorstki
    Commented May 27, 2014 at 12:39
  • 24
    the setMonth() method mutates the date it's called on so surely you need to do the following to not change jan312009 var jan312009 = new Date(2009, 0, 31); var eightMonthsFromJan312009 = new Date(jan312009.getTime()); eightMonthsFromJan312009.setMonth(jan312009.getMonth()+8);
    – tazmaniax
    Commented Jul 4, 2017 at 16:27
  • 22
    It's important to emphasize that both date and newDate will contain the same date after this code is run. IOW date is modified by this code.
    – Roger
    Commented Feb 13, 2020 at 14:34
133

I took a look at the datejs and stripped out the code necessary to add months to a date handling edge cases (leap year, shorter months, etc):

Date.isLeapYear = function (year) { 
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)); 
};

Date.getDaysInMonth = function (year, month) {
    return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};

Date.prototype.isLeapYear = function () { 
    return Date.isLeapYear(this.getFullYear()); 
};

Date.prototype.getDaysInMonth = function () { 
    return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};

Date.prototype.addMonths = function (value) {
    var n = this.getDate();
    this.setDate(1);
    this.setMonth(this.getMonth() + value);
    this.setDate(Math.min(n, this.getDaysInMonth()));
    return this;
};

This will add "addMonths()" function to any javascript date object that should handle edge cases. Thanks to Coolite Inc!

Use:

var myDate = new Date("01/31/2012");
var result1 = myDate.addMonths(1);

var myDate2 = new Date("01/31/2011");
var result2 = myDate2.addMonths(1);

->> newDate.addMonths -> mydate.addMonths

result1 = "Feb 29 2012"

result2 = "Feb 28 2011"

5
  • 8
    Is this the only solution that considers for example this: Oct 31 2011 + 1 ?
    – chris
    Commented Sep 26, 2013 at 7:11
  • 1
    easier IMHO finding days in month is return new Date(year, month+1, 0).getDate(); So also the need for isLeapYear() falls. Also not clear the need for not-Prototype-Methods
    – Daniel
    Commented Nov 14, 2014 at 9:43
  • Interesting Daniel that you can put a 0 day to get the previous month.
    – Jazaret
    Commented Nov 14, 2014 at 17:15
  • 2
    There is a problem with this solution: jsfiddle.net/nikoudel/mdzaddeh Suppose initial date is "2011-03-11T00:00:00Z" and local timezone is Helsinki (GMT+2). When adding six months, an hour gets lost because of daylight savings time: Sat, 10 Sep 2011 23:00:00 GMT. Commented Mar 31, 2016 at 10:40
  • See this response when a non extending prototype solution is needed, as discussed in stackoverflow.com/questions/14034180/…
    – Miquel
    Commented Mar 31, 2016 at 11:03
20

I would highly recommend taking a look at datejs. With it's api, it becomes drop dead simple to add a month (and lots of other date functionality):

var one_month_from_your_date = your_date_object.add(1).month();

What's nice about datejs is that it handles edge cases, because technically you can do this using the native Date object and it's attached methods. But you end up pulling your hair out over edge cases, which datejs has taken care of for you.

Plus it's open source!

1
  • Too many files for a simple task. NOPE Commented Jul 2 at 17:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.