#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7

    How-to check if holidays within range


    Ok. I got down the script to count how many days between 2 dates, and not count the weekends. Now, I need to check if an array of dates may fall within this range, but not on a weekend. The holiday dates will be stored in a MySQL db, and I figure I'll be to create the JS array from that. I just need to know to take an array, and check each date within the array to see if 1, the date is within the range, and 2, the date is not a weekend. This is what I have so far, and thanks for any input.

    Code:
    function dateCount() {
      var startDate = new Date(document.getElementById('start').value);
      var endDate = new Date(document.getElementById('end').value);
      var holiDates = new Array();
      holiDates[0] = "01/16/2013";
      holiDates[1] = "05/16/2013";
      holiDates[2] = "01/16/2014";
      var fullCount = 0;
      while (startDate <= endDate) {
        var day = startDate.getDay();
        if(day != 0 && day != 6) {
          fullCount++; 
        }
        startDate.setDate(startDate.getDate() + 1); 
      }
      var holDays = ????;
      document.getElementById("holDays").innerHTML = holDays || 0;
      var vacDays = fullCount - holDays;
      document.getElementById("vacDays").innerHTML = vacDays || 0;
    }
    Last edited by Triple_Nothing; March 30th, 2013 at 01:05 PM.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,183
    Rep Power
    184
    Put the array keys through a for() loop; that way your function can validate each of the dates, in each array's key.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    Best to change a date to a time? Or would I just do similar to my while loop and check if it's greater than/less than n add to a count?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    I think I basically got it, but when I select a range that include only 1 of the dates in the array, I am getting a count of three. Any idea?
    Code:
                            function dateCount() {
                              var startDate = new Date(document.getElementById('start').value);
                              var endDate = new Date(document.getElementById('end').value);
                              var holiDates = [];
                              holiDates[0] = new Date("01/16/2013");
                              holiDates[1] = new Date("03/20/2013");
                              holiDates[2] = new Date("01/16/2014");
                              var fullCount = 0;
                              var paidCount = 0;
                              while (startDate <= endDate) {
                                var day = startDate.getDay();
                                var length = holiDates.length;
                                for (var i = 0; i < length; i++) {
                                  if(holiDates[i] >= startDate && holiDates[i] <= endDate) {
                                    paidCount++;
                                  }
                                }
                                if(day != 0 && day != 6) {
                                  fullCount++; 
                                }
                                startDate.setDate(startDate.getDate() + 1); 
                              }
                              //var holDays = ????;
                              document.getElementById("holDays").innerHTML = paidCount || 0;
                              //var vacDays = fullCount - holDays;
                              document.getElementById("vacDays").innerHTML = fullCount || 0;
                            }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    I got it. I just couldn't have my for() within the while, otherwise every round until startDay becomes later will add to count. Thanks all.

    EDIT: And I guess while I'm at it, any suggestion on simplifying or cleaning up my code?

    Code:
                            function dateCount() {
                              var startDate = new Date(document.getElementById('start').value);
                              var endDate = new Date(document.getElementById('end').value);
                              var holiDates = [];
                              holiDates[0] = new Date("01/16/2013");
                              holiDates[1] = new Date("03/20/2013");
                              holiDates[2] = new Date("01/16/2014");
                              var length = holiDates.length;
                              var fullCount = 0;
                              var paidCount = 0;
                              for (var i = 0; i < length; i++) {
                                if(holiDates[i] >= startDate && holiDates[i] <= endDate) {
                                  paidCount++;
                                }
                              }
                              while (startDate <= endDate) {
                                var day = startDate.getDay();
                                if(day != 0 && day != 6) {
                                  fullCount++; 
                                }
                                startDate.setDate(startDate.getDate() + 1); 
                              }
                              document.getElementById("holDays").innerHTML = paidCount || 0;
                              var vacDays = fullCount - paidCount;
                              document.getElementById("vacDays").innerHTML = vacDays || 0;
                            }

IMN logo majestic logo threadwatch logo seochat tools logo