gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:40:21 25 4
gpt4 key购买 nike

在我的表单中,我有 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);

<强> Demo

关于javascript - 如何从开始日期中的单独字段添加天/周以获取日期选择器中的结束日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com