3
I need to add a date, from a "purchase date" and days in expiration date.
In the console.log
I’m capturing the data I need, but at the time the date sum is generating an incorrect date.
function addDays(){
var data = $('#dataCompra').val();
var dias = $('#tipoproduto').val();
console.log(dias);
var result = new Date(data);
console.log(data);
console.log(dias);
result.setDate(result.getDate() + dias);
console.log(result);
$('#dataValidade').val(result);
};
</script>
On a date that was to add 5 days, which was the value typed and captured on the console, it calculates an unexpected date:
5
2016-10-10
5
Date 2017-01-03T23:00:00.000Z
Can someone help me?
I’m sorry, but I couldn’t execute that method...
– Jeverson Cerri
Does it work with onblur? I need to enter a purchase date and onblur calls the method, calculates the date and the method needs to return in the dataValidade field...
– Jeverson Cerri
Does the solution need to use pure javascript? Or can it be in Jquery? I tested this function on the console and it worked
– Vinicius Colares
Now yes, I just adjusted the variables to my form and it worked right.. Another question, I can calculate the date from a date typed, using the Moment?
– Jeverson Cerri
Yes, the Moment.js library is SUPER complete, you would only need to parse the date typed, and ensure that the user would enter a valid date
– Vinicius Colares
Cool... I’ll study here then.. helped me a lot.. Thank you
– Jeverson Cerri