My date is in this format 2008-01-01
(yyyy-mm-dd). I would like to add months to add format such that if I have 2008-01-01 + 13 would give me 2009-02-01
. How can this be done in javascript?
Asked
Active
Viewed 79 times
0

guagay_wk
- 26,337
- 54
- 186
- 295
-
2Possible duplicate of [Javascript function to add X months to a date](http://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date) – Daniel Oct 30 '15 at 13:14
-
May I ask why the negative vote? What is wrong with the question? Please explain so that I can improve. – guagay_wk Oct 30 '15 at 14:00
4 Answers
1
var d = new Date('2008-01-01');
d.setMonth( d.getMonth() + 1 );
alert( d.getFullYear()+'-'+(d.getMonth() + 1 ) + '-'+ d.getDate());

Shailendra Sharma
- 6,976
- 2
- 28
- 48
1
You can add months to your date this way.
new Date(new Date(myDate).setMonth(myDate.getMonth()+13));

Simimmo
- 658
- 1
- 6
- 15
1
Simple as that:
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 13);

alvarodms
- 671
- 5
- 8
-
Thanks. Upvoted. How to I assign `2008-01-01` to `myDate`? Tried `myDate = '2008-01-01';` but it didn't work. – guagay_wk Oct 30 '15 at 13:24
-
0
This is my own answer.
var moment = require('moment')
var myDate = new Date("2008-01-01");
myDate.setMonth(myDate.getMonth() + 13);
var answer = moment(myDate).format('YYYY-MM-DD');

guagay_wk
- 26,337
- 54
- 186
- 295