如何从开始日期的单独字段添加天/周以在日期选择器中获取结束日期?

how to add days / weeks from separate field in start date to get end date in datepicker?

在我的表单中,我有 3 个字段。

<input name="course_duration" id="course_duration" type="text"> (A numeric value which denotes 'Weeks'
<input name="course_start" id="course_start" type="text">
<input name="course_end" id="course_end" type="text">

我正在使用日期选择器 jquery,我想通过从 course_duration 字段 + course_start 日期[=中插入的值添加周数来自动填充 course_end 日期13=]

目前我正在使用以下 javascript 代码弹出日历并手动选择 course_start 和 course_end 的日期。

<script type="text/javascript">
$(document).ready(function() {
$('#course_start').datepicker({dateFormat: 'yy-mm-dd'});
$('#course_end').datepicker({dateFormat: 'yy-mm-dd'});
});
</script> 

JSFIDDLE

// Get the week from course_duration
var weeks = $('#course_duration').val();

// Get the selected date from startDate
var startDate = $('#course_start').datepicker('getDate');
var d = new Date(startDate);

// Add weeks to the selected date, multiply with 7 to get days
var newDate = new Date(d.getFullYear(), d.getMonth(), d.getDate() + weeks * 7);

// Set the new date to the course endDate
$('#course_end').datepicker('setDate', newDate);

Demo