【问题标题】:jQuery - DateTimePicker - Make time required to remove the current timejQuery - DateTimePicker - 删除当前时间所需的时间
【发布时间】:2020-11-05 15:51:23
【问题描述】:

所以我使用的是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 分钟增量,或者进行所需的时间选择以及日期。

有什么想法吗?

【问题讨论】:

    标签: jquery datetimepicker


    【解决方案1】:

    好的,我终于找到了解决方案,这里是更新的代码:

    我还进行了一些进一步的更改,因此如果今天的日期不在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'
             ]              
        });         
    
      }); 
    

    【讨论】:

    • 现在只需要一种方法,让它现在在选择日期和时间之前写入当前时间......
    【解决方案2】:

    使用此代码:

    var currentDate = new Date();
    var minutes = currentDate.getMinutes();
    var m = (Math.ceil(minutes/30) * 30) % 60;
    currentDate.setMinutes(m);
    

    然后将currentDate 设置为您的日期时间选择器defaultDate 或某种方法setDate

    【讨论】:

    • 谢谢@step - 你将如何使用我提供的代码?
    • 在询问之前,请使用您首先解决的任何问题的文档。
    • 我已经检查过但仍然不是更明智的@step
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2021-03-03
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多