JSFiddle - React, Tailwind, and code Playground
by Manu Vashishtha
HTML
<input type="date" id="start_date" placeholder="Start Date"/>
<input type="number" id="days" placeholder="Days"/>
<input type="date" id="end_date" placeholder="End Date" readonly/>
JavaScript
;(function($, window, document, undefined){
$("#days").on("change", function(){
debugger;
var date = new Date($("#start_date").val()),
days = parseInt($("#days").val(), 10);
if(!isNaN(date.getTime())){
date.setDate(date.getDate() + days);
$("#end_date").val(date.toInputFormat());
} else {
alert("Invalid Date");
}
});
//From: http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
Date.prototype.toInputFormat = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
var _t=(dd[1]?dd:"0"+dd[0])+'/'+(mm[1]?mm:"0"+mm[0])+'/'+yyyy
alert(_t);
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
};
})(jQuery, this, document);