6

The question is simple, how can I add days to a date in YYYY-MM-DD format?

For example, increment a date given in this format: "2013-02-11" to "2013-02-12"?

d-_-b
  • 19,976
  • 37
  • 134
  • 224
Simulator88
  • 585
  • 6
  • 11
  • 26

4 Answers4

3
date      = new Date('2013-02-11');
next_date = new Date(date.setDate(date.getDate() + 1));

here's a demo http://jsfiddle.net/MEptb/

3

Hope below code will helpful to you

function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

var myDate = new Date('2013-02-11');

var newDate = addDays(myDate,5);
Mallikarjuna Reddy
  • 1,204
  • 2
  • 20
  • 33
2

Something like this :

    var date = new Date('2013-02-11');        
    /* Add nr of days*/
    date.setDate(date.getDate() + 1);

    alert(date.toString());

I hope it helps.

Dumitru Chirutac
  • 607
  • 2
  • 8
  • 27
  • Perfect, now i have to find to way to convert date.toString() to yyyy-mm-dd format! – Simulator88 Feb 12 '13 at 22:26
  • You can create your own format like this: var newDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); SEE [DEMO](http://jsfiddle.net/ducwidget/AayZw/) – Dumitru Chirutac Feb 12 '13 at 23:43
0

Below function is to Add number of days to today's Date It returns the Incremented Date in YYYY-MM-DD format @param noofDays - Specify number of days to increment. 365, for 1 year.

function addDaysToCurrentDate(noofDays){
date      = new Date();
next_date = new Date(date.setDate(date.getDate() + noofDays));
var IncrementedDate = next_date.toISOString().slice(0, 10);
console.log("Incremented Date " +IncrementedDate );
return IncrementedDate;
}
Fukasi
  • 1
  • 1