Bootstrap DateTimePicker使用minDate和maxDate返回错误的时间

时间:2018-11-08 03:58:00

标签: jquery twitter-bootstrap datepicker

你好,我正在尝试使引导datetimepicker插件与minDate和maxDate一起正常使用,这是我的代码:

var $el                      = $('#dateFrom');
var baseDate                = "2018-11-20";
var defaultDate             = moment(baseDate + " 13:30:00");
var min                     = moment(baseDate + " 08:00:00");
var max                     = moment(baseDate + " 22:00:00");

if($el.data('DateTimePicker')){ //function needs to reset it on each call
    $el.data('DateTimePicker').destroy();
}

$el.val(defaultDate.format("HH:mm"));

var config = {
  format:"HH:mm",
  defaultDate:defaultDate,
  minDate:min,
  maxDate:max
}

$el.datetimepicker(config);

这里的问题是它默认将时间设置为minDate而不是将其设置为defaultDate,这是一个示例http://jsfiddle.net/imurphy/bte731ux/38/

1 个答案:

答案 0 :(得分:0)

这似乎可以满足您的要求:http://jsfiddle.net/gcrb2xsm/

var $el          = $('#dateFrom');
var baseDate     = "2018-11-20";
var defaultDate  = moment(baseDate + " 13:30:00");

if($el.data('DateTimePicker')){
    $el.data('DateTimePicker').destroy();
}

$el.val(defaultDate.format("HH:mm"));
$('#should').text("Initian value should be " + defaultDate.format("HH:mm"));

var config = {
  format:"HH:mm",
  defaultDate:defaultDate,
  disabledTimeIntervals: [[moment({ h: 0 }), moment({ h: 8 })], [moment({ h: 22 }), moment({ h: 24 })]]
}

$el.datetimepicker(config);

基本上,disabledTimeIntervals是在尝试将时间选择限制为特定时间范围时使用的选项。您需要为每个范围添加一对开始时间和结束时间,以排除。因此,上面显示的值是:

[
    [moment({ h: 0 }), moment({ h: 8 })] /* disable from midnight to 8 AM */, 
    [moment({ h: 22 }), moment({ h: 24 }) /* disable from 10 PM to midnight */]
]