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.
But exclusions are coming like: Nan
– pnet
Had made a fiddle and worked right using
console.log
but should show right withalert
– Pedro Camara Junior
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?
– pnet
I did it in the fiddle and see how it is. It doesn’t work: link
– pnet
@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 astring
. Note the following message in the browser consoleUncaught TypeError: dt_exclusao.getDate is not a function
– Pedro Camara Junior
I know, but the format that is received by the function parameter is string and I’ve already given a
Date.parse()
. onestringTodate()
and nothing happened. I’m not able to parse from one format to another.– pnet
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– Pedro Camara Junior