# Calculate No Of Days Excluding Weekends

• 10-19-2012, 05:35 PM
jimsan
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
• 10-22-2012, 09:15 AM
JMRKER
Quote:

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.
:)

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09689 seconds
• Memory Usage 2,351KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• process_templates_complete