0

我知道daterangepicker在禁用日期範圍中有其功能。但是這次我需要禁用時間範圍。具體爲上午12:00至下午1:00。如果我選擇了上午11:00至下午2:00,計算出的總小時數將從禁用時間範圍剝離1小時2小時。這可能是這個選擇器嗎?如果沒有,是否有解決方法?作爲首發,我有thisBootstrap Daterangepicker時間減法

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

HTTP://http//jsfiddle.net/onelazy/amtxsf0x/鏈接無法正常工作。 –

+0

我們對此深感抱歉,我編輯的鏈接改成了工作一個 – OneLazy

+0

嘿OneLazy,您的問題已得到解決......檢查編輯答案(把所有的腳本到您的代碼,並嘗試)。 –

回答

2

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; 
    } 
} 
+0

InBreakTime總是錯誤的爲什麼?或者我忽略了什麼?這裏是一個更新的小提琴:http://jsfiddle.net/amtxsf0x/2/ – OneLazy

+0

根據您的更新,無需採取'var breakEnd = moment(「12:59 PM」,「h:mm A」);'保持原樣...然後嘗試。 InBreakTime將是真實的,如果您選擇時間範圍'11:00至2:00「,它將在扣除您的休息時間後給出總共2.6小時... –

+0

先生我試過選擇02/10/2017上午8:30 - 02/10/2017 3:30 PM,但它不斷返回7.0它不剝離1小時 – OneLazy