Please see the answer for your question below. You can also pass negative values to the function addDays() to get previous dates.
Date.prototype.addDays = function (num) {
var value = this.valueOf();
value += 86400000 * num;
return new Date(value);
}
$('#n_days').change(function(){
var $datepicker = $('#start_date');
var $datepicker1 = $('#end_date');
var selectedDate = $datepicker.val();
var result = new Date(selectedDate);
var dateval = result.getDate();
var ndays = $('#n_days').val();
numdays = Number(ndays);
var expirationDate = result.addDays(numdays);
end_date1 = new Date(expirationDate);
var day = ("0" + end_date1.getDate()).slice(-2);
var month = ("0" + (end_date1.getMonth() + 1)).slice(-2);
var year = end_date1.getFullYear();
var newdate = year + '-' + month + '-' + day;
$datepicker1.val(newdate);
});
<!-- begin snippet: js hide: false console: true babel: false -->
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" >
</script>
<div class="container">
<form id="form1" name="form1" action="add_con.php" method="post">
<table id="t01">
<tr>
<td>
<div class="row">
<div class="col-40">
<label for="startdate">start date:</label>
</div>
<div class="col-60">
<input type="date" id="start_date" name="start_date" size="20" >
</div>
</div>
</td>
</tr>
<td> <div class="row">
<div class="col-40">
<label for="n_daysa">No. of days:</label>
</div>
<div class="col-60">
<font face="Cambria" style="font-size: 12pt">
<font size="4">
<input type="number" id="n_days" name="n_days" min=0 placeholder="Enter no. of days.." required size="20" style="font-size:14px"></font>
</font>
</div>
</div>
</td>
<td>
<div class="row">
<div class="col-40">
<label for="end_datea"> end date:</label>
</div>
<div class="col-60">
<font face="Cambria" style="font-size: 12pt">
<input type="date" id="end_date" name="end_date" placeholder="End date.." required size="20" style="font-size:14px">
</font>
</div>
</div>
</td>
</tr>
</table>
<br><br><br>
<hr color="#003399">
<!-- start second table-->
<table class="table1" style="width:1209;" height="151">
<tr>
<th height="21" width="34"><font color="#003399">Day</font></th>
<th height="21" width="295"><font color="#003399">Date</font></th>
</tr>
<tr>
<td height="1" width="34"><b>2</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date2" name="date2" size="10" style="width: 90%; "></font></td>
</tr>
<tr>
<td height="1" width="34"><b>3</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date3" name="date3" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>4</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date4" name="date4" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>5</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date5" name="date5" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>6</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date6" name="date6" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>7</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date7" name="date7" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>8</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date8" name="date8" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>9</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date9" name="date9" size="10" style="width: 90%;"></font></td>
</tr>
<tr>
<td height="1" width="34"><b>10</b></td>
<td width="200" height="1">
<font face="Cambria" style="font-size: 11pt">
<input type="date" id="date10" name="date10" size="10" style="width: 90%;"></font></td>
</tr>
</table>
<!-- end second table -->
<div class="row">
<p align="center">
<font face="Cambria">
<input type="submit" value="Submit"></font>
<font face="Cambria">
<input type="reset" value="Reset" name="reset"> </font>
</div>
</form>
</div>
This is working fine.
One more thing to ask, how can we enable other date fields based on the Input from 'Number of days'. I have another 9 date fields (date2--date10) and I want to auto-fill the date as per the entry in 'Number of days'. For example: If user inputs start date- 03/10/2020 and number of days- 5 and end-date - 08/10/2020
then it should display like:
date2- 04/10/2020,
date3- 05/10/2020,
date4- 06/10/2020,
date5- 07/10/2020,
date6- 08/10/2020,
date7- disabled,
date8- disabled,
date9- disabled,
date10- disabled
added the new date fields in your code. Please suggest..Thanks.
`, `...`, ...)