因此,我使用的是jQuery DateTimePicker,目前我有以下设置:
jQuery(document).ready(function($) {
jQuery.datetimepicker.setLocale('en');
jQuery('#datepicker').datetimepicker({
beforeShowDay: $.datepicker.noWeekends,
format: 'd/m/Y H:i a',
minDate: 0,
minTime: 0,
step: "30",
allowTimes:[
'09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30',
'13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00'
]
});
});
正如你所看到的,你不能选择过去的日期或时间,时间选择只在工作日的晚上9-5点…
我的问题是,如果我只是选择今天的日期,它放在带有当前时间戳的日期中,我需要以某种方式将其向上舍入到下一个30分钟增量,或者选择所需的时间以及日期。
有什么想法吗?
发布于 2020-11-06 04:20:55
好的,我最终找到了一个解决方案,下面是更新后的代码:
我还做了一些进一步的更改,所以如果今天的日期在allowTimes
之外,那么它是不可选的。
由于它还设置了当前时间,因此如果您选择将来的日期,它会将时间设置为'09:00‘,这是allowTimes
中的第一个可用时间
var checkPastTime = function(currentDateTime) {
var d = new Date();
var todayDate = d.getDate();
if (currentDateTime.getDate() == todayDate) { // check today date
this.setOptions({
minTime: d.getHours() + '1:00' //here pass current time hour ;
});
} else
this.setOptions({
minTime: false
});
if(currentDateTime.getDate() == todayDate && d.getHours() >= 17) {
this.setOptions({
minDate:'+1970/01/02',
minTime: '09:00',
defaultTime: '09:00'
});
}
};
jQuery(document).ready(function($) {
jQuery.datetimepicker.setLocale('en');
var d = new Date();
var todayDate = d.getDate();
if(todayDate == d.getDay() + 1) {
var ahead = d.getHours() + 1;
} else {
var ahead = '9';
}
jQuery('#datepicker').datetimepicker({
//beforeShowDay: $.datepicker.noWeekends,
format: 'd/m/Y H:i a',
minDate: 0,
onChangeDateTime: checkPastTime,
onShow: checkPastTime,
defaultTime: ahead + ':00',
step: "30",
allowTimes:[
'09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30',
'13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00'
]
});
});
发布于 2020-11-06 00:12:45
使用以下代码:
var currentDate = new Date();
var minutes = currentDate.getMinutes();
var m = (Math.ceil(minutes/30) * 30) % 60;
currentDate.setMinutes(m);
然后将currentDate
设置为defaultDate
或某个方法setDate
https://stackoverflow.com/questions/64700663
复制相似问题