Search code examples
javascriptjquerydaterangepickerbootstrap-daterangepicker

Bootstrap Daterangepicker time subtraction


I know daterangepicker has its function in disabling date range. But this time I need to disable time range. Specifically 12:00 AM - 1:00 PM. If I selected 11:00 AM - 2:00 PM the calculated total hours would be 2 hours stripping 1hr from the disable time range. Is this possible this picker? If not, is there a work around ? As starter, I have this

$(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);
    });
});

Solution

  • EDITED

    $(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;
      }
    }