SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Block Sundays and Specific Dates in jQuery UI datepicker

    I am using the jquery ui datepicker and have it working just fine.

    What I am trying to do is block Sundays and a list of specific dates. I have accomplished each task individually but I can't figure out how to do both at the same time. (not sure how to combine the functions).

    If you can offer assistance, it would be MUCH appreciated.

    My code to blog specific dates

    Code:
    var disabledDays = ["2-20-2012"];
    
    /* utility functions */
    function nationalDays(date) {
      var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
      //console.log('Checking (raw): ' + m + '-' + d + '-' + y);
      for (i = 0; i < disabledDays.length; i++) {
        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
          //console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
          return [false];
        }
      }
      //console.log('good:  ' + (m+1) + '-' + d + '-' + y);
      return [true];
    }
    
    function noHolidays(date) {
      return nationalDays(date);
    }
    
    	$(function() {
    		$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd', minDate: +1, beforeShowDay: noHolidays });
    	});
    here is the code I use to just block Sundays (it's inline with the datepicker function

    Code:
    $(function() {
    		$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd', minDate: +1, beforeShowDay: function(date) {
            var day = date.getDay();
            return [(day != 0), ''];
        } });
    	});
    How can I accomplish both of these actions? Thanks in advance for any help

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    First we'll modify the function so that it returns true or false, and not an array instead.
    That will allow us to use the result from the nationalDays and the sundays function, from the noHolidays function.

    Code javascript:
    function nationalDays(date) {
      var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
      //console.log('Checking (raw): ' + m + '-' + d + '-' + y);
      for (i = 0; i < disabledDays.length; i++) {
        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
          //console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
          return true;
        }
      }
      //console.log('good:  ' + (m+1) + '-' + d + '-' + y);
      return false;
    }
     
    function sundays(date) {
        var day = date.getDay();
        return (day === 0);
    }
     
    function noHolidays(date) {
      return [!sundays(date) && !nationalDays(date)];
    }
    Last edited by paul_wilkins; Feb 13, 2012 at 15:08.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thanks for the help. Do you know of a way to allow for wildcards in some dates in the "disabledDays" list?

    Basically, some days I want to block every year such as christmas, new years, 4th of july etc so I would want to be able to use a date like "7-4-*" or some other format if needed such as "7 4 *".

    Let me know if this is possible Thanks again for the help!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mtwebguy View Post
    Awesome, thanks for the help. Do you know of a way to allow for wildcards in some dates in the "disabledDays" list?

    Basically, some days I want to block every year such as christmas, new years, 4th of july etc so I would want to be able to use a date like "7-4-*" or some other format if needed such as "7 4 *".

    Let me know if this is possible Thanks again for the help!
    One way could be something like this:

    Code javascript:
    function isHoliday(date, holidays) {
        var parts,
            dateArray = [],
            i;
        for (i = 0; i < holidays.length; i += 1) {
            parts = holidays[i].split('/');
            if (parts.length === 2) {
                dateArray.push(new Date(date.getFullYear(), parts[0], parts[1] - 1).getTime());
            } else if (parts.length === 3) {
                dateArray.push(new Date(holidays[i]).getTime());
            } else {
                return false;
            }
        }
        return ($.inArray(date.getTime(), dateArray) !== -1);
    }
     
    function workDays(date) {
        var holidays = ['12/25', '1/1', '5/10/2011', '5/25'];
        return [!sundays(date) && !isHoliday(date, holidays)];
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •