JQuery Datepicker - 根据当前时间设置MinDate

时间:2015-01-15 10:29:31

标签: javascript jquery datepicker

我使用JQuery Datepicker为客户选择交货日期。我希望能够检查客户是否在中午之前订购,如果是,那么第二天可以提供。如果他们在中午之后订购,那么第二天就无法送货,因此这一天是不可选择的。

我有一些代码可以检查当前时间,但是如何在顶部的设置中将此值添加到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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分: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;
}

此外,您可以轻松分离功能。它更容易阅读,您始终可以从函数中调用函数。希望这有帮助!

答案 1 :(得分:0)

删除var块中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
}