gpt4 book ai didi

javascript - 将 datetimepicker 的最小日期设置为另一个 datetimepicker 的选定日期

转载 作者:行者123 更新时间:2023-12-01 03:21:22 28 4
gpt4 key购买 nike

我有两个文本框:txtETCdatefrom 和 txtETCdateto 分别具有类 datetimepicker1 和 datetimepicker2:

<asp:TextBox ID="txtETCdatefrom" runat="server" CssClass="form-control datetimepicker1 col-md-6"></asp:TextBox>
<asp:TextBox ID="txtETCdateto" runat="server" CssClass="col-md-6 datetimepicker2 form-control"></asp:TextBox>

我在两个类上都使用仅日期格式的日期时间选择器(如果我将 minDate 设置为任何特定日期或时刻(),则工作正常)。我想将 datetimepicker2 的最小日期设置为 datetimepicker1 的选定值或 txtETCdatefrom 的值。我的日期时间选择器代码是:

$('.datetimepicker1').datetimepicker({
format: 'DD/MM/YYYY',
minDate: moment(),
});
$('.datetimepicker2').datetimepicker({
format: 'DD/MM/YYYY',
//minDate: moment(),
});

我搜索了这个并找到了 datepicker 的答案(使用 onSelect() 函数):

$('.datetimepicker1').datetimepicker({
format: 'DD/MM/YYYY',
minDate: moment(),
onSelect: function (date) {
var date1 = $('.datetimepicker1').datetimepicker('getDate');
var date = new Date(Date.parse(date1));
date.setDate(date.getDate() + 1);
var newDate = date.toDateString();
newDate = new Date(Date.parse(newDate));
$('.datetimepicker2').datetimepicker("option", "minDate", newDate);
}
});
$('.datetimepicker2').datetimepicker({
format: 'DD/MM/YYYY',
//minDate: moment(),
});

但它不适用于日期时间选择器。我还尝试过 beforeShow() 函数,例如:

$('.datetimepicker1').datetimepicker({
format: 'DD/MM/YYYY',
minDate: moment(),
});
$('.datetimepicker2').datetimepicker({
format: 'DD/MM/YYYY',
//minDate: moment(),
beforeShow: function () {
$(".datetimepicker2").datetimepicker("option", {
minDate: $(".datetimepicker1").datetimepicker('getDate')
});
}
});

但一切都是徒劳。还尝试了 onSelectDate() 和 onShow() 函数,如建议的 Set max and min datetime in jquery datetimepicker不知道我做错了什么。在代码中包含这些函数后,日期时间选择器不会显示。所有帮助将不胜感激。谢谢。

最佳答案

我遵循 datepicker official website 中的文档。

$(function () {

$('#datetimepicker1').datetimepicker({
format: 'DD/MM/YYYY',
minDate: moment(),
});


$('#datetimepicker2').datetimepicker({
format: 'DD/MM/YYYY',
});

$("#datetimepicker1").on("dp.change", function (e) {
var oldDate = new Date(e.date);
var newDate = new Date();
newDate.setDate(oldDate.getDate() + 1);

$('#datetimepicker2').data("DateTimePicker").minDate(newDate);
});

});
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.bootcss.com/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" rel="stylesheet"/>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>

<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>

<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>


<script src="https://cdn.bootcss.com/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

关于javascript - 将 datetimepicker 的最小日期设置为另一个 datetimepicker 的选定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180198/

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