1

I just want to add 45 months to the given date in JavaScript. I have tried this:

var startDateFormat = new Date(2018, 11, 24); // 11 is for January starts from 0
var addDate = startDateFormat.setMonth(startDateFormat.getMonth() + 45); //want to add 45 months
console.log(new Date(addDate).getFullYear() + "-" + new Date(addDate).getMonth() + 1 + "-" + new Date(addDate).getUTCDate())

But the result is 2019-101-23. Can anybody help me why this is happening?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Sandip Nag
  • 968
  • 2
  • 18
  • 34
  • Try this [link](https://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript) – Nareen Babu Dec 24 '18 at 10:14
  • 1
    The comment on the first line is wrong. January is 0, December is 11. Why call a variable `startDateFormat` when it is not a format, but a date. – trincot Dec 24 '18 at 10:14

4 Answers4

3

There are a few problems there:

  • setMonth modifies the state of the instance you call it on, you don't usually use its return value.
  • The + 1 you're doing after getMonth is adding a "1" to the string. If you want it numerically, group it with the getMonth.
  • Don't combine UTC methods and non-UTC methods, use UTC in all of the calls or none of them.
  • You don't have to create new dates all over the place, you can just use the first one.
  • Not quite sure what your comment saying "11 is for January starts from 0" is meant to mean. 11 is December, not January.

So:

var dt = new Date(2018, 11, 24); // 11 is for January starts from 0
dt.setMonth(dt.getMonth() + 45); //want to add 45 months
console.log(dt.getFullYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate());
// Note parens ----------------------^-----------------^
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You should place new Date(addDate).getMonth() + 1 in parentheses. You are building a string and since you don't provide explicit precedence, first new Date(addDate).getMonth() gets added, then 1 gets concatenated to the string.

Try this:

var startDateFormat = new Date(2018, 11, 24);

var addDate = startDateFormat.setMonth(startDateFormat.getMonth() + 45);

console.log(new Date(addDate).getFullYear() + "-" + (new Date(addDate).getMonth() + 1) + "-" + new Date(addDate).getUTCDate())

Or template string:

var startDateFormat = new Date(2018, 11, 24);

var addDate = startDateFormat.setMonth(startDateFormat.getMonth() + 45);

console.log(`${new Date(addDate).getFullYear()}-${new Date(addDate).getMonth() + 1}-${new Date(addDate).getUTCDate()}`);
Alex G
  • 1,897
  • 2
  • 10
  • 15
  • There's no point to all those `new Date`s, and in fact, you can't reliably use `new Date(dateInstance)` to clone a date cross-browser. – T.J. Crowder Dec 24 '18 at 10:16
  • @T.J.Crowder True, I was simply answering OPs question about why he sees this output. – Alex G Dec 24 '18 at 10:18
0

45 months, means 3 years + 9 months. so use javascript divider and modulus operators something like this..

        var startDateFormat = new Date(2018, 11, 24); // 11 is for January starts from 0

    var year = startDateFormat.setYear(startDateFormat.getFullYear() + (45 / 12));

    if ((startDateFormat.getMonth() + 1 + (45 % 12)) > 12)
    {
        year = new Date(year).setYear(new Date(year).getFullYear() + 1);


        var month = startDateFormat.setMonth(startDateFormat.getMonth() + (45 % 12) - 12 + 1);
    }
    else
    {
        var month = startDateFormat.setMonth(startDateFormat.getMonth() + (45 % 12) + 1);
    }


    console.log(new Date(year).getFullYear() + "-" + new Date(month).getMonth() + "-" + new Date(month).getDate())

Thanks

Obaid
  • 2,563
  • 17
  • 15
  • 1
    There's no need for that at all. JavaScript's `Date` object correctly handles wrapping the units. If you add three months to December 2018, you get March 2019 *per spec*. – T.J. Crowder Dec 24 '18 at 11:33
-2

Looking at your code, it seems like you need to play with dates a lot in your project. In that case, I suggest you to try momentjs library which makes playing with dates really easy.

e.g

moment([2010, 0, 31]);                  // January 31
moment([2010, 0, 31]).add(1, 'months'); // February 28

similarly, it had tons easy to use features. for more refer, http://momentjs.com/ and for documentation on the same visit http://momentjs.com/docs/

Akshay Rajput
  • 1,978
  • 1
  • 12
  • 22