我对javascript和jQuery很新,但我设法得到了Trent Richardson的jQuery Timepicker,http://trentrichardson.com/examples/timepicker/。
我添加了限制用户可以选择的日期的约束,但我现在需要根据当前时间更改这些限制。如果当前时间是在下午3点之前添加一天,如果在下午3点之后添加2天。
我有它工作,但我觉得代码有点笨重而且考虑不周。有谁知道是否有更优雅的方式实现这一目标? 提前感谢:)
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: ' @ '});
}
答案 0 :(得分:1)
我认为我说的唯一区别是minDate
属性。使用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: ' @ '});
答案 1 :(得分:0)
这是你的代码。
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: ' @ '});
}
这里优雅(至少更优雅)
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
}
然后当然将选项传递给timepicker