I have a standard datepicker with a Start and End date. I want the End date to increase by 1 year based on what the user selects as the start date.
Here is my Javascript code.
$("#startDate").on("change", function (e) {
$("#endDate").val($(this).val());
});
A solution is to create a date object from the start input value and increment the year by one,
afterward, you can format and feed the modified date back in the endDate input:
$("#startDate").on("change", function(e) {
var d = new Date($(this).val())
d.setFullYear(d.getFullYear() + 1)
$("#endDate").val(d.toISOString().split('T')[0]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<p>
start
</p>
<input type='date' id='startDate' placeholder='start' />
</div>
<div>
<p>
end
</p>
<input type='date' id='endDate' />
</div>