twitter-bootstrap 如何在 bootstrap datetimepicker 中限制最小-最大时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28940527/
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
How to limit Min-Max Time in bootstrap datetimepicker
提问by Samus
How to limit
如何限制
I'm trying to limit the time a user can select from this plugin: https://github.com/Eonasdan/bootstrap-datetimepicker
我试图限制用户可以从此插件中选择的时间:https: //github.com/Eonasdan/bootstrap-datetimepicker
I've searched around this site and google for similar questions, but users generally ask for the validation to work between one known period and another known period. e.g 9:00 - 21:00 2015/03/09 9:00 - 2015/03/10 21:00. If a user inputs 2015/03/09 23:00 the validation will pass this because the time is within those two min/max periods.
我在这个网站和谷歌上搜索过类似的问题,但用户通常要求验证在一个已知时期和另一个已知时期之间工作。例如 9:00 - 21:00 2015/03/09 9:00 - 2015/03/10 21:00。如果用户输入 2015/03/09 23:00 验证将通过,因为时间在这两个最小/最大周期内。
I need it to work as a booking system works, so that each day is limited between those times. e.g if a user selects 2015/03/09 23:00 or 2015/03/10 5:00 then the validation won't pass, it will only pass on any day between 9:00 & 21:00.
我需要它像预订系统一样工作,以便每天在这些时间之间受到限制。例如,如果用户选择 2015/03/09 23:00 或 2015/03/10 5:00 那么验证不会通过,它只会在 9:00 和 21:00 之间的任何一天通过。
$('.datetimepicker').datetimepicker({
stepping: 30,
minDate: moment({hour: 9, minute: 30}),
format: 'YYYY-MM-DD HH:mm'
});
This starts the min date for today at 9:30am, but if I was to choose any day after today I would be able to select for example 4am - anything under 9:30am
这从今天上午 9 点 30 分开始,但如果我要选择今天之后的任何一天,我将能够选择例如凌晨 4 点 - 上午 9 点 30 分以下的任何时间
回答by K C
Use the enabledHours property:
使用 enabledHours 属性:
$('.datetimepicker').datetimepicker({
stepping: 30,
enabledHours: [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
format: 'YYYY-MM-DD HH:mm'
});
This way it should allow any hours between 9:00 and 20:30, but won't allow anything from 21:00 on. More at http://eonasdan.github.io/bootstrap-datetimepicker/Options/#endisabledhours.
这样它应该允许 9:00 和 20:30 之间的任何时间,但不允许从 21:00 开始。更多在http://eonasdan.github.io/bootstrap-datetimepicker/Options/#endisabledhours。

