2015-01-15 44 views
0

我使用JQuery Datepicker爲客戶選擇交貨日期。我希望能夠檢查客戶是否在中午之前訂購,如果是,可以在第二天交貨。如果他們在中午之後訂購,第二天交貨不可用,所以這一天是不可選擇的。JQuery Datepicker - 根據當前時間設置MinDate

我有一些代碼來檢查當前時間,但如何將此值添加到MinDate中的頂部設置?

謝謝!

<div class="delivery-date"> 
 
    <p> 
 
    <label for="date">Select a date for delivery below:</label> 
 
    <input id="date" type="text" name="properties[delivery-date]" readonly="readonly" style="background:white; width:30%" class="required" data-error="Please choose a delivery date." /> 
 
    </p> 
 
</div> 
 

 
<script> 
 
jQuery(function() { 
 
    jQuery("#date").datepicker({ 
 
    // minDate: new Date(((new Date).getTime() + 49 * 60 * 60 * 1000)), 
 
    minDate: checkBeforeNoon, 
 
    maxDate: "+2M", // show up to 2 months 
 
    dateFormat: 'dd/mm/yy', 
 
    beforeShowDay: available_delivery_dates 
 
    }); 
 
}); 
 
    
 

 
/*========== check time ==========*/ 
 

 
// if time before 12pm, offer next day delivery 
 

 
function checkBeforeNoon(nextDayDelivery){ 
 

 
    var startTime = '12:00 AM'; 
 
    var endTime = '12:00 PM'; 
 

 
    var curr_time = getval(); 
 

 

 
    if (get24Hr(curr_time) > get24Hr(startTime) && get24Hr(curr_time) < get24Hr(endTime)) { 
 
     // before 12pm - next day delivery available 
 
     var nextDayDelivery = '+1d'; 
 

 
    } else { 
 
    // after 12pm - next day delivery unavailable 
 
    var nextDayDelivery = '+2d'; 
 
    } 
 

 
    function get24Hr(time){ 
 
     var hours = Number(time.match(/^(\d+)/)[1]); 
 
     var AMPM = time.match(/\s(.*)$/)[1]; 
 
     if(AMPM == "PM" && hours<12) hours = hours+12; 
 
     if(AMPM == "AM" && hours==12) hours = hours-12; 
 
     
 
     var minutes = Number(time.match(/:(\d+)/)[1]); 
 
     hours = hours*100+minutes; 
 
     console.log(time +" - "+hours); 
 
     return hours; 
 
    } 
 

 
    function getval() { 
 
     var currentTime = new Date() 
 
     var hours = currentTime.getHours() 
 
     var minutes = currentTime.getMinutes() 
 

 
     if (minutes < 10) minutes = "0" + minutes; 
 

 
     var suffix = "AM"; 
 
     if (hours >= 12) { 
 
      suffix = "PM"; 
 
      hours = hours - 12; 
 
     } 
 
     if (hours == 0) { 
 
      hours = 12; 
 
     } 
 
     var current_time = hours + ":" + minutes + " " + suffix; 
 

 
     return current_time; 
 

 
    } 
 
} 
 

 
/*========== Make sundays always unavailable ==========*/ 
 

 

 
function available_delivery_dates(date) { 
 

 
    var sunday = 0; // unavailable for delivery 
 
    var mon = 1 
 
    var tue = 2; 
 
    var wed = 3; 
 
    var thu = 4; 
 
    var fri = 5; 
 
    var sat = 6; 
 
    
 
    var day_of_week = date.getDay(); 
 
    var not_sun = day_of_week > 0; 
 
    
 
    if(not_sun){ 
 
    var day = date.getDate(); 
 
    return [true, '']; 
 
    } 
 
    else{ 
 
    // all else - do not allow 
 
    return [false, ' ', 'Delivery is unavailable on this day']; 
 
    } 
 

 
    
 
    
 
} 
 
    
 
</script>

+0

你設置'minDate'的功能,而不是返回值,你需要調用你的函數...'的minDate:checkBeforeNoon(),' –

+0

謝謝,但並沒有工作 – casiokid

+0

也'beforeShowDay: available_delivery_dates()' – kRiZ

回答

1

你已經做到了。你只需要添加一個返回函數來檢查它是否是中午。如果您想告訴客戶現在有一天可以運送,您可以通過在您的功能中添加日誌來完成。下面是修改代碼:

http://jsfiddle.net/graphicfreedom/L3tz8243/1/

function checkBeforeNoon(nextDayDelivery){ 
     var startTime = '12:00 AM'; 
     var endTime = '12:00 PM'; 
     var curr_time = getval(); 

    if (get24Hr(curr_time) > get24Hr(startTime) && get24Hr(curr_time) < get24Hr(endTime)) { 
     // before 12pm - next day delivery available 
     var nextDayDelivery = '+1d'; 
     $("#log").html('Next day delivery available! Order before noon!'); //show response to user 
    } else { 
    // after 12pm - next day delivery unavailable 
    var nextDayDelivery = '+2d'; 
     $("#log").html('Next day delivery NOT available! It is already past noon :('); //show response to user 
    } 
    return nextDayDelivery; 
} 

此外,你可以很容易分開的功能。它更容易閱讀,並且您可以隨時從函數調用函數。希望這可以幫助!

+0

非常感謝! :) – casiokid

0

刪除 nextDayDelivery之前的if-else塊,因爲您將重新聲明它。然後返回nextDayDelivery。此外,在getVal()方法中修復缺少的分號是一個好主意。

function checkBeforeNoon(nextDayDelivery) { 

    var startTime = '12:00 AM'; 
    var endTime = '12:00 PM'; 

    var curr_time = getval(); 


    if (get24Hr(curr_time) > get24Hr(startTime) && get24Hr(curr_time) < get24Hr(endTime)) { 
     // before 12pm - next day delivery available 
     nextDayDelivery = '+1d'; // REMOVE var FROM HERE 

    } else { 
     // after 12pm - next day delivery unavailable 
     nextDayDelivery = '+2d'; // REMOVE var FROM HERE 
    } 

    function get24Hr(time) { 
     var hours = Number(time.match(/^(\d+)/)[1]); 
     var AMPM = time.match(/\s(.*)$/)[1]; 
     if (AMPM == "PM" && hours < 12) hours = hours + 12; 
     if (AMPM == "AM" && hours == 12) hours = hours - 12; 

     var minutes = Number(time.match(/:(\d+)/)[1]); 
     hours = hours * 100 + minutes; 
     console.log(time + " - " + hours); 
     return hours; 
    } 

    function getval() { 
     // ADD MISSING SEMI-COLONS ON THE FOLLOWING 3 LINES 
     var currentTime = new Date(); 
     var hours = currentTime.getHours(); 
     var minutes = currentTime.getMinutes(); 

     if (minutes < 10) minutes = "0" + minutes; 

     var suffix = "AM"; 
     if (hours >= 12) { 
      suffix = "PM"; 
      hours = hours - 12; 
     } 
     if (hours == 0) { 
      hours = 12; 
     } 
     var current_time = hours + ":" + minutes + " " + suffix; 

     return current_time; 
    } 

    return nextDayDelivery; // ADD RETURN STATEMENT 
} 
相關問題