Thread: Weekday count

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

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    Weekday count


    Howdy. I have built a script to count number of days within 2 selected days on a time off page just fine, except it counted weekends. So, sadly a simple startDate - endDate won't work here. I am working on this one function to take its place, and just count weekdays, but I never get an update on my count printed to the page.

    I feel the script errors/hangs somewhere, because with my original script, after a date is selected from the calendar, the calendar disappears, and the count gets updated. But with this script, the calendar inserts the date into the textbox, but remains displayed.

    I have tried to use both date variables for the Date() function.
    firstDate = mm/dd/yyyy
    startDate = yyyy mm dd

    Code:
    function dateCount() {
      var firstDate = new Date(document.getElementById('start').value);
      var firstDateSplit = firstDate.value.split(/[^0-9]+/);
      var startDate = new Date(firstDateSplit[2], firstDateSplit[0], firstDateSplit[1]); 
      var secondDate = new Date(document.getElementById('end').value);
      var secondDateSplit = secondDate.value.split(/[^0-9]+/);
      var endDate = new Date(secondDateSplit[2], secondDateSplit[0], secondDateSplit[1]); 
      var workingdays = 0;
      while (startDate <= endDate) {
        var day = startDate.getDay();
        if(day != 0 && day != 6) {
          workingdays++; 
        }
        startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 1); 
      }
      document.getElementById("vacDays").innerHTML = workingdays || 0;
    }
    Code:
    Vacation Days: You will be using <SPAN id="vacDays">0</SPAN> Day(s) of vacation.<BR />
    Last edited by Triple_Nothing; March 29th, 2013 at 10:41 AM.
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    Check your browser's error console. In Firefox, I get this error message:
    TypeError: firstDate.value is undefined
    I recommend you use this reference: https://developer.mozilla.org/en-US/...l_Objects/Date

    Also inside the while loop, I would recommend using setDate() on startDate instead of creating a new date object.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Well, this is my past function which successfully counted all days between two dates. The first/last day are set in the exact same manner and worked. Any idea why this would be any different?
    Code:
    function dateRange() {
      var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
      var firstDate = new Date(document.getElementById('start').value);
      var secondDate = new Date(document.getElementById('end').value);
      var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)-1));
      document.getElementById("vacDays").innerHTML = diffDays || 0;
    }
    And as far as the setDate instead, I assume I would use it for both start/end as well, but since the dates can be in random months, how would this be defined? Sorry, not one to work in Javascript all that much...
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    I'm saying that not everything in JavaScript has a "value" property, so this
    Code:
      var firstDateSplit = firstDate.value.split(/[^0-9]+/);
    // ...other code...
      var secondDateSplit = secondDate.value.split(/[^0-9]+/);
    should be
    Code:
      var firstDateSplit = firstDate.toString().split(/[^0-9]+/);
    // ...other code...
      var secondDateSplit = secondDate.toString().split(/[^0-9]+/);
    Though actually that won't work as you intend either, because the default output format is different from what you seem to be expecting. There is no need to call the the Date constructor so many times. You can remove two of the calls by doing this:
    Code:
      var firstDateSplit = document.getElementById('start').value.split(/[^0-9]+/);
      var startDate = new Date(firstDateSplit[2], firstDateSplit[0], firstDateSplit[1]);
      var secondDateSplit = document.getElementById('end').value.split(/[^0-9]+/);
      var endDate = new Date(secondDateSplit[2], secondDateSplit[0], secondDateSplit[1]);
    By the way, the regex you're using could be shortened from "/[^0-9]+/" to "/\D+/".

    Originally Posted by Triple_Nothing
    And as far as the setDate instead, I assume I would use it for both start/end as well, but since the dates can be in random months, how would this be defined?
    Not quite. I'm saying that you would use setDate to increment the date by one day, not when setting the date initially.
    Code:
    startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 1);
    would be simplified to this:
    Code:
    startDate.setDate( startDate.getDate() + 1);
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Aight. I got it working, and I'm pretty sure it's exactly what I started with, other than editing that one line to use the setDate. Thanks for your help with this step!
    Code:
    function dateCount() {
      var startDate = new Date(document.getElementById('start').value);
      var endDate = new Date(document.getElementById('end').value);
      var workingdays = 0;
      while (startDate <= endDate) {
        var day = startDate.getDay();
        if(day != 0 && day != 6) {
          workingdays++; 
        }
        startDate.setDate( startDate.getDate() + 1); 
      }
      document.getElementById("vacDays").innerHTML = workingdays || 0;
    }
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    You're welcome.

    Yes, you could pass the value of the text field directly to the date constructor instead of using split(). However, I should give you a word of warning that it seems that browsers are not required to support that date format, so there's no guarantee that all browsers will support that format.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Thanks for the note. I believe this company is limited to IE, so as long as that works, I hope I'm ok. ^_^

IMN logo majestic logo threadwatch logo seochat tools logo