Gussy Rodriguez Gussy Rodriguez - 3 months ago 6
Javascript Question

How can I add 1 day to this JS function?

function AgregarDias(date, days) {
var result = new Date(date);
var dateFormated = result.toISOString().substr(0,10);
//dateFormated.setDate(dateFormated.getDate() + days);
return dateFormated;
}


Any ideas, i've tried with too much ways

Answer

This should work:

function AgregarDias(date, days) {
    var dateFormated = new Date(date);
    dateFormated.setDate(dateFormated.getDate() + days);
    return dateFormated.toISOString().substr(0,10);
}

I am not sure why you are doing:

var dateFormated = result.toISOString().substr(0,10);

You should be able to work directly with the new Date object.

Comments