简体   繁体   English

jQuery timepicker中的if语句

[英]if statement within jQuery timepicker

I'm pretty new to javascript and jQuery, but I managed to get Trent Richardson's jQuery Timepicker, http://trentrichardson.com/examples/timepicker/ . 我对javascript和jQuery还是很陌生,但是我设法获得了Trent Richardson的jQuery Timepicker, http: //trentrichardson.com/examples/timepicker/。

I added constraints for limiting dates the user can select, but I now need to change those limits based on the current time. 我添加了限制用户可以选择的日期的限制,但是现在我需要根据当前时间更改这些限制。 Adding a day if the current time is before 3pm, and adding 2 days if it's after 3pm. 如果当前时间在下午3点之前,则添加一天;如果下午3点之后,则添加2天。

I have it working, but I feel that the code is a little bulky and poorly thought out. 我有它的工作,但我觉得代码有点笨重,考虑不周。 Does anyone know if there's a more elegant way of achieving this? 有谁知道是否有更优雅的方法来实现这一目标? Thank in advance :) 预先感谢 :)

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if(hours > 15){
    //document.write("too late buster!")
    $('#datepicker').datetimepicker({
    minDate: +2,
    maxDate: +30, 
    dateFormat: "d/M", 
    timeFormat: 'hh:mm', 
    hourMin: 11, 
    hourMax: 19, 
    hourGrid: 4, 
    minuteGrid: 10, 
    stepMinute: 5, 
    separator: ' @ '});
} else {
    $('#datepicker').datetimepicker({
    minDate: +1,
    maxDate: +30, 
    dateFormat: "d/M", 
    timeFormat: 'hh:mm', 
    hourMin: 11, 
    hourMax: 19, 
    hourGrid: 4, 
    minuteGrid: 10, 
    stepMinute: 5, 
    separator: ' @ '});
}

I think I'm right in saying that the only difference is the minDate property. 我想我说对了,唯一的区别是minDate属性。 It's easy to set this conditionally with a ternary expression using the conditional operator : 使用条件运算符使用三元表达式很容易地设置此条件:

$('#datepicker').datetimepicker({
minDate: (new Date().getHours() > 15 ? +2 : +1),
maxDate: +30, 
dateFormat: "d/M", 
timeFormat: 'hh:mm', 
hourMin: 11, 
hourMax: 19, 
hourGrid: 4, 
minuteGrid: 10, 
stepMinute: 5, 
separator: ' @ '});

Here is your code. 这是您的代码。

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if(hours > 15){
    //document.write("too late buster!")
    $('#datepicker').datetimepicker({
    minDate: +2,
    maxDate: +30, 
    dateFormat: "d/M", 
    timeFormat: 'hh:mm', 
    hourMin: 11, 
    hourMax: 19, 
    hourGrid: 4, 
    minuteGrid: 10, 
    stepMinute: 5, 
    separator: ' @ '});
} else {
    $('#datepicker').datetimepicker({
    minDate: +1,
    maxDate: +30, 
    dateFormat: "d/M", 
    timeFormat: 'hh:mm', 
    hourMin: 11, 
    hourMax: 19, 
    hourGrid: 4, 
    minuteGrid: 10, 
    stepMinute: 5, 
    separator: ' @ '});
}

Here is elegant (at least more elegant) 这里很优雅(至少更优雅)

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var options = {
    minDate: +1,
    maxDate: +30, 
    dateFormat: "d/M", 
    timeFormat: 'hh:mm', 
    hourMin: 11, 
    hourMax: 19, 
    hourGrid: 4, 
    minuteGrid: 10, 
    stepMinute: 5, 
    separator: ' @ '
}
if (hours > 15) {
    options.minDate = +2
}

Then of course pass the options into the timepicker 然后当然将选项传递到时间选择器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM