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
  • 21,536
  • 40
  • 150
  • 256
Simulator88
  • 617
  • 6
  • 12
  • 27

5 Answers5

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

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

3

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
  • 617
  • 2
  • 8
  • 28
  • 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
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,212
  • 2
  • 20
  • 33
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
  • 2
0

With the date parameter you can add your required date and you can add days with the addDays() function:

    var start_date = new Date('2013-02-11');
    var next_date_update = addDays(start_date,1);
    var next_date = new Date(next_date_update).toLocaleDateString('en-CA');

    if(next_date!='Invalid Date')
    {
       var final_date = next_date;
       console.log(final_date);
    }

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

hope it will help you out

Pelmered
  • 2,727
  • 21
  • 22
deepak
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 03 '23 at 15:49