繁体   English   中英

如何在开始日期的单独字段中添加天数/周数以在datepicker中获得结束日期?

[英]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">

我正在使用datepicker jQuery,我想通过从在course_duration字段中插入的值加上course_start日期来增加星期数来自动填写course_end日期

目前,我正在使用以下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);

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM