I've a form with fields .daysfromtoday
and #datefromtoday
.
I would like to enter a number of days in .daysfromtoday and update #datefromtoday with the date yyyy-mm-dd format. (I know that there are other posts about this subject, but, if possible, I would like someone helps me with code I've written, to learn more)
HTML:
<input type="text" class="daysfromtoday" />
<input type="text" id="datefromtoday" name="delay" />
javascript:
$(document).ready(function (){
$(".daysfromtoday").on('change', function(){//
var waitdays = $( ".daysfromtoday" ).val(); //take value from field .daysfromtoday
var enddate = new Date();
enddate.setDate(enddate.getDate() + waitdays);
var yyyy = enddate.getFullYear().toString();
var mm = (enddate.getMonth()+1).toString();
var dd = enddate.getDate().toString();
document.getElementById("datefromtoday").value = yyyy + '-' + mm + '-' + dd; //outuput
});
});
Problems: (and solutions)
I get weird results: today is
2016-02-26
, if I enter 100, I get2087-7-17
The result format should have the 0 (zeros): 2016-03-08