Thread: Calculate No Of Days Excluding Weekends

1. Member
Join Date
Oct 2012
Posts
2

Calculate No Of Days Excluding Weekends

<html>
`<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>`

<body>

<script>
</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. Originally Posted by jimsan
...
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.

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

Posting Permissions

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