Add days to a date within the method and return it with a new date range

4

I tried to make a function to add to that date, days coming from a parameter passed in the function. Below the function:

function montaDataSubstituicaoPrestador(){

    var exclusao = new Date('2014-03-14T23:54:00');
    var novadata = new Date();
    var p = 60;

    //lblTeste
    novadata.setDate(exclusao.getDate() + p);

    alert(exclusao);
    alert(p);
    alert(novadata);
}

I changed the function, because the date that should have come by parameter, was giving error of NaN . I removed the second parameter too, which would be the summed days, but this was correct, I changed to the "P", but that's not the problem. It turns out that no alert(exclusao) is coming with NaN and of course novadata is also coming like this. How do I add days to a date in js? I took it from colleague Morrison, here .

    
asked by anonymous 24.11.2015 / 20:48

2 answers

2

You're adding 60 days based on the current date new Date() , you need to use setDate() using the opt-out date.

var exclusao = new Date('2014-03-14T23:54:00');
var novaData = exclusao;
novaData.setDate(novaData.getDate() + 60);
    
24.11.2015 / 20:58
0

Here is a function that I use in JAVASCRIPT ...

function somaDias(dt,qtd)
{
    var dt1 = dt.split("/");
    var hj1 = dt1[2]+"-"+dt1[1]+"-"+dt1[0];
    var dtat = new Date(hj1);
    dtat.setDate(dtat.getDate());
    var myDate = new Date(hj1);
    myDate.setDate(myDate.getDate() + (qtd+1));
    var ano = myDate.getFullYear();
    var dia = myDate.getDate(); if(dia<10){dia='0'+dia};
    var mes = (myDate.getMonth()+1); if(mes<10){mes='0'+mes}        
    return (dia+"/"+mes+"/"+ano);
}

// Informe a data e a quantidade de dias que deseja somar.
alert(somaDias('31/03/2017',1));
    
01.04.2017 / 16:41