8
I need to make a function increment X days to a date. The problem is that the date is coming as string
in that format: dd/mm/yyyy
and at the time of assigning the X days to that date, says it does not recognize the var that will be incremented in X days, as: var_tal is not a function
. How do I proceed? here is the example in fiddle.
That way it doesn’t work:
var dt_exclusao = '26/11/2015';
var periodo = 60;
var nova_data = new Date(dt_exclusao);
//alert(dt_exclusao.setDate(dt_exclusao.getDate() + periodo));
alert(nova_data);
alert(periodo);
nova_data.setDate(nova_data.getDate() + periodo);
alert(nova_data);
But how it works, it means that it is the past format that lies the problem:
var dt_exclusao = '2015-11-26T00:00:00';
var periodo = 60;
var nova_data = new Date(dt_exclusao);
//alert(dt_exclusao.setDate(dt_exclusao.getDate() + periodo));
alert(nova_data);
alert(periodo);
nova_data.setDate(nova_data.getDate() + periodo);
alert(nova_data);
The fiddle worked, but the page didn’t. The function on the page is like this:
function montaDataSubstituicaoPrestador(dt_exclusao, periodo){
var arrData = dt_exclusao.split('/');
var novaData = new Date(arrData[2] + '-' + arrData[1] + '-' + arrData[0]);
alert(arrData);
alert(dt_exclusao);
alert(periodo);
alert(novaData);
novaData.setDate(novaData.getDate() + periodo);
alert(novaData);
}
The big difference is that fiddle
I place in my hand the dt_exclusao
and in the function I receive. Date mounted on novaData
comes like this: NaN
. The arrData
is correct, type: 26,11,2015, but the setup gives error NaN
.
No point in passing
dd/mm/yyyy
fornew Date()
. Have you found an alternative format that is accepted, why not use it?– bfavaretto
@bfavaretto, the date already comes from another function that picks up from the bank in this format
dd/mm/yyyy
all my problem is that I’m not getting to turn from a format(string - dd/mm/yyyy)
for the accepted. This is the reason for the post.– pnet
Break the value with split as suggested by Pedro
– bfavaretto
In your fiddle, there is no Date object, it won’t even work.
– Ivan Ferrer
In reality this is another question, even for the same function. Here I ask regarding how to move from string to date, so it has not been answered yet.
– pnet
No? Pedro Camara Junior explains exactly how to do.
– bfavaretto
Hadn’t seen the discussion here, it’s working now @pnet ?
– Pedro Camara Junior
But here’s the fiddle working: http://jsfiddle.net/ivanferrer/8hz20pkL/
– Ivan Ferrer