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

Asked

Viewed 213 times

5

I tried to do 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 come by parameter, was giving error NaN. I removed the second parameter too, which would be the days added up, but this one was correct, I switched to the "P", but that’s not the problem. It turns out that in alert(exclusao) there’s a NaN and of course novadata It’s also coming like this. How do I add days to a date on js? I took that from colleague Morrison, here.

2 answers

3


You are adding 60 days based on the current date new Date(), you need to use the setDate() using the date of exclusion.

var exclusao = new Date('2014-03-14T23:54:00');
var novaData = exclusao;
novaData.setDate(novaData.getDate() + 60);
  • But exclusions are coming like: Nan

  • Had made a fiddle and worked right using console.log but should show right with alert

  • With me is not working, I marked as a response, because a test I did worked, but with date mounted, but the code did not work and so I decided to put this comment. Is it because the date is coming without the hour part?

  • I did it in the fiddle and see how it is. It doesn’t work: link

  • @pnet To be able to add days on the date you need to have an object of type Date. in the example you made in the fiddle is using a string. Note the following message in the browser console Uncaught TypeError: dt_exclusao.getDate is not a function

  • I know, but the format that is received by the function parameter is string and I’ve already given a Date.parse(). one stringTodate() and nothing happened. I’m not able to parse from one format to another.

  • In fact, it will only work with one variable Date valid. If you are in doubt how to format, you can ask a question here in the OS. Beforehand, you can read in the type reference Date here

Show 2 more comments

0

Here’s a role 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));
  • Jetro, Welcome! Avoid placing duplicate answers. If the questions are about the same problem mark them as duplicates. See you soon!

  • Hello, Sergio. I don’t understand your comment. Where can I see if the question is about "Same problem" ?

  • Hi! You gave the same answer here as in this other question: https://answall.com/a/194415/129 Hence my comment. Usually the same answer (without changing a comma) is very likely that the question is the same and then should be marked as duplicate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.