Bootstrap datetimepicker mindate
Bootstrap datetimepicker mindate
我在我的网络应用程序中使用 https://github.com/Eonasdan/bootstrap-datetimepicker 来显示日期时间选择器。
我有 2 个控件 - startDate and endDate
我想将 'endDate'
的 minDate
设置为大于 'startDate'
。
本插件提供'minDate'
选项。
但是,如何从另一个 datetimepicker 字段设置它的值?
我试着做了如下。
$('#start-date-time-datepicker').datetimepicker({
minDate: moment(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$('#end-date-time-datepicker').datetimepicker({
minDate: $('#start-date-time-datepicker').val(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
但它不起作用。
有人可以指点一下吗?
您可以使用 $("#start-date-time-datepicker").data("DateTimePicker").date()
或 $("#start-date-time-datepicker").find("input").val()
来获得您想要的值。
这是一个简单的演示。
var $start = $('#start-date-time-datepicker'),
$end1 = $('#end-date-time-datepicker1'),
$end2 = $('#end-date-time-datepicker2')
$start.datetimepicker({
minDate: moment(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end1.datetimepicker({
minDate: $start.data("DateTimePicker").date(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end2.datetimepicker({
// note that this is not in a recognized RFC2822 or ISO format
// $("#start-date-time-datepicker").find("input").val()
minDate: new Date($start.find("input").val()),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$start.on('dp.change', function(){
// $start input change event
var startDate = $(this).data("DateTimePicker").date();
// set $end1 and $end2 to $start's date and set the minDate
$end1.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
$end2.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<div class='input-group date' id='start-date-time-datepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
您需要绑定日期选择器
$(function () {
$('#start_date').datetimepicker({
// custom config
});
$('#end_date').datetimepicker({
// custom config
});
$("#start_date").on("dp.change", function (e) {
$('#end_date').data("DateTimePicker").minDate(e.date);
});
$("#end_date").on("dp.change", function (e) {
$('#start_date').data("DateTimePicker").maxDate(e.date);
});
});
参考:http://eonasdan.github.io/bootstrap-datetimepicker/#linked-pickers
我在我的网络应用程序中使用 https://github.com/Eonasdan/bootstrap-datetimepicker 来显示日期时间选择器。
我有 2 个控件 - startDate and endDate
我想将 'endDate'
的 minDate
设置为大于 'startDate'
。
本插件提供'minDate'
选项。
但是,如何从另一个 datetimepicker 字段设置它的值?
我试着做了如下。
$('#start-date-time-datepicker').datetimepicker({
minDate: moment(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$('#end-date-time-datepicker').datetimepicker({
minDate: $('#start-date-time-datepicker').val(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
但它不起作用。
有人可以指点一下吗?
您可以使用 $("#start-date-time-datepicker").data("DateTimePicker").date()
或 $("#start-date-time-datepicker").find("input").val()
来获得您想要的值。
这是一个简单的演示。
var $start = $('#start-date-time-datepicker'),
$end1 = $('#end-date-time-datepicker1'),
$end2 = $('#end-date-time-datepicker2')
$start.datetimepicker({
minDate: moment(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end1.datetimepicker({
minDate: $start.data("DateTimePicker").date(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end2.datetimepicker({
// note that this is not in a recognized RFC2822 or ISO format
// $("#start-date-time-datepicker").find("input").val()
minDate: new Date($start.find("input").val()),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$start.on('dp.change', function(){
// $start input change event
var startDate = $(this).data("DateTimePicker").date();
// set $end1 and $end2 to $start's date and set the minDate
$end1.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
$end2.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<div class='input-group date' id='start-date-time-datepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
您需要绑定日期选择器
$(function () {
$('#start_date').datetimepicker({
// custom config
});
$('#end_date').datetimepicker({
// custom config
});
$("#start_date").on("dp.change", function (e) {
$('#end_date').data("DateTimePicker").minDate(e.date);
});
$("#end_date").on("dp.change", function (e) {
$('#start_date').data("DateTimePicker").maxDate(e.date);
});
});
参考:http://eonasdan.github.io/bootstrap-datetimepicker/#linked-pickers