Jquery date picker: disable next day after midday

I’m using Gravity Forms and the Gravity Forms WooCommerce Product Add-Ons plugin to add a delivery date to a product.

I want the customer to be able to select the following day if it is before 12 midday, but not if it is after midday (the products are only available in the UK).

Read More

This is what I’m using to grey out previous dates and also to disable Sundays, as there’s no delivery on Sundays:

<script type="text/javascript">
jQuery(document).ready(function($) { 
$(".datepicker").datepicker({ minDate:1, beforeShowDay: function(date) 
{ var day = date.getDay(); return [(day != 0), '']; } }); });
</script>

I found this question/answer: JQuery UI Datepicker Disbale Next Day After 12pm

But I’m not sure how to isolate the code I need from that solution to just disable next-day delivery after midday, and not add all the other functionality. If anyone’s able to help me out I would be hugely grateful!

Related posts

2 comments

  1. I use this tactic for local pickup date picker options. It’s been running on several client sites for several months with no complaints on accuracy!

    <script>
    
        function partial_day_cutoff(date) {
    
          // default to no orders in the same day
          var minDate = 1;
    
          // get the current date stamp
          var date = new Date();
    
          // get the current hour (24 hr format)
          var current_hour = date.getHours();
    
          // if the current timestamp is greater than 12, 
          // change the minDate to no orders within this and the next day
          if( current_hour > 12 ) {
              minDate = 2;
          }
    
          return minDate;
    
        }
    
    
        jQuery(document).ready(function($){
    
          var minDate = partial_day_cutoff();
    
          $("#thedate").datepicker({
    
            minDate : minDate,
    
          });   
    
        });
    
    </script>
    
  2. First you need to get the current time of UK.
    Then after you need to validate for sunday and midday time. If current time is ok (as per your conditions) then you need to set(visible) datepicker.

    create file getUKTime.php

        <?php 
        $userCountry = getCountryCode();
        if($userCountry=='GB') {
            date_default_timezone_set('Europe/London');
            $data=array("message"=>"success", "date"=>date("l Y-m-d g:i a"));
            echo json_encode($data);
        }else {
            $data=array("message"=>"The products are only available in the UK!");
            echo json_encode($data);    
        }
        /* get current user location iformation that either the user from UK or not */
        function getCountryCode() {
            $headers = array('Content-Type: application/json');
            $ip = $_SERVER["REMOTE_ADDR"];
            $deep_detect = TRUE;
            if ($deep_detect) {
              if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)){
                 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
              }
              if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)){
                 $ip = $_SERVER['HTTP_CLIENT_IP'];
              }
            }
            $url = 'http://www.geoplugin.net/json.gp?ip='.$ip;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result1 = curl_exec($ch);
            curl_close($ch);        
            $data=json_decode($result1, true);
            return $data['geoplugin_countryCode'];
        }
        ?>
    

    edit jquery function in your code

        <script>
            var midday = 0;// default value is 0 for diabled datepicker selction
            $.ajax({
                   url: "getUKTime.php",
                   type: "POST", data: {},
                   beforeSend: function (jqXHR, settings){if (jqXHR && jqXHR.overrideMimeType) {jqXHR.overrideMimeType("application/j-son;charset=UTF-8");}},
                   dataFilter: function (data) {var response = eval('(' + data + ')');return response;},
                   success: function (result) {
                       if(result.message=='success') {
                           var getDates=result.date.split(" ");
                           if(getDates[3]=='pm'){  midday=1;/*set it '1' to enable datepicker (UK time is in "pm" then datepicker will be enable)*/ }
                       }else{
                          midday=0;
                          alert(result.message); 
               }
                       $( "#Datepicker1").datepicker({
                           minDate: 0,
                           dateFormat: 'mm/dd/yy',
                           inline: true,
                           dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                           beforeShowDay: disableDays,
                        });
                   },
                   error: function (jqXHR, textStatus, errorThrown) {},
                   complete: function (jqXHR, textStatus){}
                }); 
    
            function disableDays(date){ 
                  if(midday==0) {return [(day > 0), ''];}
                  var day = date.getDay();
                  return [(day > 0), ''];
              }; 
        </script>
        <input type="text" id="Datepicker1" />
    

Comments are closed.