Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?

前端 未结 11 673
無奈伤痛
無奈伤痛 2020-11-22 04:02

I use a datepicker for choosing an appointment day. I already set the date range to be only for the next month. That works fine. I want to exclude Saturdays and Sundays f

相关标签:
11条回答
  • 2020-11-22 04:30

    There is the beforeShowDay option, which takes a function to be called for each date, returning true if the date is allowed or false if it is not. From the docs:


    beforeShowDay

    The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable and 1 equal to a CSS class name(s) or '' for the default presentation. It is called for each day in the datepicker before is it displayed.

    Display some national holidays in the datepicker.

    $(".selector").datepicker({ beforeShowDay: nationalDays})   
    
    natDays = [
      [1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'],
      [4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'],
      [7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'],
      [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']
    ];
    
    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
          if (date.getMonth() == natDays[i][0] - 1
              && date.getDate() == natDays[i][1]) {
            return [false, natDays[i][2] + '_day'];
          }
        }
      return [true, ''];
    }
    

    One built in function exists, called noWeekends, that prevents the selection of weekend days.

    $(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })
    

    To combine the two, you could do something like (assuming the nationalDays function from above):

    $(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})   
    
    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }
    

    Update: Note that as of jQuery UI 1.8.19, the beforeShowDay option also accepts an optional third paremeter, a popup tooltip

    0 讨论(0)
  • 2020-11-22 04:31

    In this version, month, day, and year determines which days to block on the calendar.

    $(document).ready(function (){
      var d         = new Date();
      var natDays   = [[1,1,2009],[1,1,2010],[12,31,2010],[1,19,2009]];
    
      function nationalDays(date) {
        var m = date.getMonth();
        var d = date.getDate();
        var y = date.getFullYear();
    
        for (i = 0; i < natDays.length; i++) {
          if ((m == natDays[i][0] - 1) && (d == natDays[i][1]) && (y == natDays[i][2]))
          {
            return [false];
          }
        }
        return [true];
      }
      function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
          if (noWeekend[0]) {
            return nationalDays(date);
          } else {
            return noWeekend;
        }
      }
      $(function() { 
        $(".datepicker").datepicker({
    
          minDate: new Date(d.getFullYear(), 1 - 1, 1),
          maxDate: new Date(d.getFullYear()+1, 11, 31),
    
          hideIfNoPrevNext: true,
          beforeShowDay: noWeekendsOrHolidays,
         });
      });
    });
    
    0 讨论(0)
  • 2020-11-22 04:35
    $("#selector").datepicker({ beforeShowDay: highlightDays });
    
    ...
    
    var dates = [new Date("1/1/2011"), new Date("1/2/2011")];
    
    function highlightDays(date) {
    
        for (var i = 0; i < dates.length; i++) {
            if (date - dates[i] == 0) {
                return [true,'', 'TOOLTIP'];
            }
        }
        return [false];
    
    }
    
    0 讨论(0)
  • 2020-11-22 04:37

    For Saturday and Sunday You can do something like this

                 $('#orderdate').datepicker({
                                   daysOfWeekDisabled: [0,6]
                     });
    
    0 讨论(0)
  • 2020-11-22 04:38

    This version of code will make u to get the holiday dates from the sql database and disable the specified date in the UI Datepicker

    
    $(document).ready(function (){
      var holiDays = (function () {
        var val = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': 'getdate.php',
            'success': function (data) {
                val = data;
            }
        });
        return val;
        })();
      var natDays = holiDays.split('');
    
      function nationalDays(date) {
        var m = date.getMonth();
        var d = date.getDate();
        var y = date.getFullYear();
    
        for (var i = 0; i ‘ natDays.length-1; i++) {
        var myDate = new Date(natDays[i]);
          if ((m == (myDate.getMonth())) && (d == (myDate.getDate())) && (y == (myDate.getFullYear())))
          {
            return [false];
          }
        }
        return [true];
      }
    
      function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
          if (noWeekend[0]) {
            return nationalDays(date);
          } else {
            return noWeekend;
        }
      }
      $(function() { 
        $("#shipdate").datepicker({
          minDate: 0,
          dateFormat: 'DD, d MM, yy',
          beforeShowDay: noWeekendsOrHolidays,
          showOn: 'button',
          buttonImage: 'images/calendar.gif', 
          buttonImageOnly: true
         });
      });
    });
    

    Create a Database in sql and put you holiday dates in MM/DD/YYYY format as Varchar Put the below contents in a file getdate.php

    
    [php]
    $sql="SELECT dates FROM holidaydates";
    $result = mysql_query($sql);
    $chkdate = $_POST['chkdate'];
    $str='';
    while($row = mysql_fetch_array($result))
    {
    $str .=$row[0].'';
    }
    echo $str;
    [/php]
    

    Happy Coding !!!! :-)

    0 讨论(0)
  • 2020-11-22 04:47

    You can use noWeekends function to disable the weekend selection

      $(function() {
         $( "#datepicker" ).datepicker({
         beforeShowDay: $.datepicker.noWeekends
         });
         });
    
    0 讨论(0)
提交回复
热议问题