www.webdeveloper.com
Results 1 to 2 of 2

Thread: Calculate No Of Days Excluding Weekends

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Calculate No Of Days Excluding Weekends

    <html>
    <head>
    `<script>
    function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
    var iWeeks, iDateDiff, iAdjust = 0;
    if (dDate2 < dDate1) return -1; // error code if dates transposed
    var iWeekday1 = dDate1.getDay(); // day of week
    var iWeekday2 = dDate2.getDay();
    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)

    if (iWeekday1 <= iWeekday2) {
    iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
    } else {
    iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
    }

    iDateDiff -= iAdjust // take into account both days on weekend

    return (iDateDiff + 1); // add 1 because dates are inclusive
    }
    </script>`

    `</head>

    <body>

    <script>
    alert(calcBusinessDays(new Date("October 19, 2012"),new Date("October 22, 2012")
    </script>

    </body>

    </html>

    with this code i am able to calculate no of days excluding weekends , now what i want to do is i want to pick up value from text field and then pass on to calcBusinessDays and then what ever is return from function want to display in new text field

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,414
    Quote Originally Posted by jimsan View Post
    ...
    with this code i am able to calculate no of days excluding weekends , now what i want to do is i want to pick up value from text field and then pass on to calcBusinessDays and then what ever is return from function want to display in new text field
    Where are the text fields? What is the new display field you mention/
    Need some more code to help.
    Are you planning to have the user enter "October 19, 2012" and the other date correctly?
    Do you have any error checks for when the user makes a mistake?

    BTW: You should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to read, copy, test and debug. It will also preserve your formatting.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles