我正在使用两个日期选择器。当我从开始日期选择器中选择日期时。我将它设置为结束日期选择器。但我试过了。它没有设置请帮助我。
$('#start-date').datetimepicker({
onShow: function(ct){
this.setOptions({
minDate: new Date()
});
},
timepicker: false,
format: 'Y/m/d h:m A',
datepicker: true,
hours12:true,
defaultSelect:true,
hideIfNoPrevNext: true,
minDate:false,
onSelectDate: function(date) {
var endDate_picker = $("#end-date");
var minDate = moment(date,"Y/m/d h:m A");
var endDate = moment(moment(minDate).toDate()).add('hours', 2);
console.log('curr date :' + date);
console.log('minDate :' + minDate);
console.log('endDate : ' + endDate);
console.log('moment : ' + moment(endDate_picker.val(),"Y/m/d h:m A").toDate());
console.log('minDate.toDate() :' + minDate.toDate());
if ((endDate_picker.val() == "")|| (moment(endDate_picker.val(),"Y/m/d h:m A").toDate()) < minDate.toDate()) {
$('#end-date').datetimepicker('setDate', endDate.toDate());
console.log('end date set :' + endDate.toDate());
}
}
});
要设置日期,请使用以下代码:
$('#end-date').datetimepicker('setDate', endDate.toDate());
答案 0 :(得分:1)
一些小错误
1.Datetimepicker格式使用
i
分钟而不是m
2.Moment.js
add()
语法有changed3.
setDate
是datetimepicker中的内部函数,不作为设置日期的公共方法公开,初始化时使用value
选项
$('#start-date').datetimepicker({
onShow: function(ct){
this.setOptions({
minDate: new Date()
});
},
timepicker: false,
format: 'Y/m/d h:i A',
datepicker: true,
hours12:true,
defaultSelect:true,
hideIfNoPrevNext: true,
minDate:false,
onSelectDate: function(date) {
var endDate_picker = $("#end-date");
var minDate = moment(date,"Y/m/d h:m A");
var endDate = moment(moment(minDate).toDate()).add(2,'hours');
if ((endDate_picker.val() == "")|| (moment(endDate_picker.val(),"Y/m/d h:m A").toDate()) < minDate.toDate()) {
$('#end-date').datetimepicker({value: endDate.toDate(), format: 'Y/m/d h:i A'});
}
}
});