I have a form with three fields, "start_date", "days", "end_date". I would like to get the end date by adding days to the start date.
My jQuery code is:
$("#days").change(function(){
var start_date = new Date($("#start_date").attr('value'));
var days = parseInt($("#days").attr('value'))-1;
var end_date = new Date(start_date);
end_date.setDate(start_date.getDate() + days);
$("#end_date").val(end_date.getFullYear() + '-' + ("0" + (end_date.getMonth() + 1)).slice(-2) + '-' + ("0" + end_date.getDate()).slice(-2));
});
In the "end_date" field I get "NaN-aN-aN".
What am I doing wrong?
start_datefield?end_date, I am asking what you get instart_date(which if its also NAN, its because the value of#start_datedoes not represent a valid date format)start_dateis not completed by the user, its a javascript Date() object that is initialised based user data. Please addconsole.log(start_date);and see what its value is.