twitter-bootstrap 如何在 bootstrap-datetimepicker.js 中设置时间选择器仅设置从上午 8:30 到晚上 8:30 的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31218245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:54:05  来源:igfitidea点击:

How to set time picker only set time from 8:30 am to 8:30 pm in bootstrap-datetimepicker.js?

jquerytwitter-bootstraptimepickerbootstrap-datetimepicker

提问by Suganthan Raj

HTML CODE:

HTML代码:

<div class="form-group">
      <label class="col-md-3 control-label" for="event_date">Start at</label>  
      <div class="col-md-8">
        <input id="event_start_date" name="event_start_date" type="text"  placeholder="" class="form-control input-md event_date" required="">
          </div>
      </div>

      <div class="form-group">
       <label class="col-md-3 control-label" for="event_date">End at</label>  
         <div class="col-md-8">
      <input id="event_end_date" name="event_end_date" type="text" placeholder="" class="form-control input-md event_date" required="">
      </div>
</div>`

JavaScript code:

JavaScript 代码:

$('#event_start_date').datetimepicker({
    minTime: "08:30:00"
});

I need to set timepicker start time as 8:30 AM to End time as 8:30 PM disable other time in time picker. How is possible please help?

我需要将时间选择器开始时间设置为上午 8:30 到结束时间为晚上 8:30 禁用其他时间选择器。怎么可能请帮助?

采纳答案by Suganthan Raj

Here i solved my problem with this code:

在这里,我用这段代码解决了我的问题:

$('.event_date').datetimepicker({
        disabledTimeIntervals: [ [ moment().hour(0), moment().hour(8).minutes(30) ], [ moment().hour(20).minutes(30), moment().hour(24) ] ]
    });

回答by Daniel Ice

The docs are lacking here, but I found that the following items are the key.

这里缺少文档,但我发现以下项目是关键。

Understand that the datetimepicker uses the moment.js library.

了解 datetimepicker 使用 moment.js 库。

The disabledTimeIntervals takes an array of arrays that define a time range.

disabledTimeIntervals 采用定义时间范围的数组数组。

[ //outer array
  [ start_time, end_time],
  [ second_start_time, second_end_time]
]

The other key point is that when you define a moment, you need to include the hour and minute, leaving the defaults caused the filter to no work for me.

另一个关键点是,当你定义一个时刻时,你需要包括小时和分钟,保留默认值会导致过滤器对我不起作用。

So if you wanted to define a shop that was open from 8:30 AM to 8:30 PM with a 30 minute, it would look as follows:

因此,如果您想定义一个营业时间为上午 8:30 到晚上 8:30 且营业时间为 30 分钟的商店,则如下所示:

$('.event_date').datetimepicker({
    disabledTimeIntervals: [
      [moment().hour(0).minutes(0), moment().hour(8).minutes(30)],
      [moment().hour(20).minutes(30), moment().hour(24).minutes(0)]
   ]
});

回答by WebQube

Haven't tried it myself but from the docs,

我自己没有尝试过,但从文档中

disabledTimeIntervals
4.14.40

Default: false
Disables time selection between the given moments