0

我知道 daterangepicker 具有禁用日期范围的功能。但是这次我需要禁用时间范围。特别是 12:00 AM - 1:00 PM。如果我选择上午 11:00 - 下午 2:00,计算的总小时数将为 2 小时,从禁用时间范围中减去 1 小时。这可能是这个选择器吗?如果没有,是否有解决方法?作为初学者,我有这个

$(function() {
    $('input[name="Date2"]').daterangepicker({
        minDate: getCurrentDateToday()+" 8:30 AM",
        dateLimit: { "days": 1},
        timePicker: true,
        timePickerIncrement: 1,
        locale: {format: 'MM/DD/YYYY h:mm A'}
    },
    function(start, end, label)
    {
        var hd = end.diff(start, 'hours');
        var md = end.diff(start, 'minutes');
        var minutesDue = md - (hd*60);
        var hoursDue = hd +"."+minutesDue;
        $('#txtTotalHrs').val(hoursDue);
    });
});
4

1 回答 1

2

已编辑

$(function() {
  $('input[name="Date2"]').daterangepicker({
      minDate: getCurrentDateToday() + " 8:30 AM",
      dateLimit: {
        "days": 1
      },
      timePicker: true,
      timePickerIncrement: 1,
      locale: {
        format: 'MM/DD/YYYY h:mm A'
      }
    },
    function(start, end, label) {

        var breakStart = "12:00 PM" , breakEnd = "12:30 PM";

      var start_end_diff = (new Date(end) - new Date(start)) / 1000 / 60;

      var break_diff = exclude_BreakTiming(start,end ,breakStart, breakEnd);

      var total_minutes = (start_end_diff - break_diff);

      $('#txtTotalHrs').val(getTimeFromMins(total_minutes));

    });
});

function getTimeFromMins(mins) {
    // do not include the first validation check if you want, for example,
    // getTimeFromMins(1530) to equal getTimeFromMins(90) (i.e. mins rollover)
    if (mins >= 24 * 60 || mins < 0) {
        throw new RangeError("Valid input should be greater than or equal to 0 and less than 1440.");
    }
    var h = mins / 60 | 0,
        m = mins % 60 | 0;
    return moment.utc().hours(h).minutes(m).format("hh:mm");
}

function getCurrentDateToday() {
  var currentDate = new Date();
  var day = currentDate.getDate()
  var month = currentDate.getMonth() + 1;
  var year = currentDate.getFullYear();
  var fullDate = month + "/" + day + "/" + year;
  return fullDate;
}

function exclude_BreakTiming (StartTime , EndTime, BreakStart, BreakEnd){
    StartTime = Date.parse('01/01/2011 ' +StartTime.format('h:mm A'));
  EndTime = Date.parse('01/01/2011 ' +EndTime.format('h:mm A'));

  var breakStart = Date.parse('01/01/2011 '+BreakStart);
  var breakEnd = Date.parse('01/01/2011 '+BreakEnd);

    if((StartTime <= breakEnd) || ((StartTime < breakStart) && (EndTime > breakEnd)))
  {
    return ((breakEnd - breakStart) /1000 /60);
  }else{
    return 0;
  }
}
于 2017-02-10T05:50:45.050 回答