only doing calculation during weekday

# Thread: only doing calculation during weekday

1. Senior Member
Join Date
Dec 1969
Posts
1,388

## only doing calculation during weekday

I have to add to the "cost" of the service if its over more than 3 days but weekends are free. IS there a mathmatical formula to find out if a day of the week is a weekday or a weekend. I was thinking of grabbing the service start date and then testing each day to see if its a weekday or weekend. This service can last 30 days so i am not so sure if thats the best route to go.<BR><BR>Matt

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: only doing calculation during weekday

Ummm...have you ever noticed the BUILTIN function named "Weekday" in VBScript?<BR><BR>Or is that what you meant by "testing each day" in the range?<BR><BR>And, by the by, what about holidays?<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Try this?

This page includes a subroutine that calculates the "business days" between two dates.<BR><BR>But CAUTION: It gives the wrong answer if either the start or end date is a Saturday or Sunday. This can be fixed, but how to do so depends on your business rules. (For example: If you really *do* provide service on a Sunday, does that then suddenly count Sunday as a business day???)<BR><BR>But treat this as a starting point, at least:<BR><BR>&LT;HTML&#062;&LT;BODY&#062;<BR><BR> &LT;%<BR>&#039 This function works by counting whole 5-day weeks<BR>&#039 between the moving back to the Saturday preceding <BR>&#039 each of the given dates, counting whole weeks between<BR>&#039 those Saturdays (which might be zero, of course!) as<BR>&#039 five days each, and then adjusting for the actual<BR>&#039 starting dates.<BR>&#039<BR>Function countBizDays( ByVal fromDate, ByVal toDate )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 we adjust both dates back to prior Saturday...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 first, get day of week (Sunday=1, etc.)<BR>&nbsp;&nbsp;&nbsp;&nbsp;fromDiff = Weekday(fromDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp;toDif f = Weekday(toDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 then subtract off day of week to get to prior Saturday<BR>&nbsp;&nbsp;&nbsp;&nbsp;fromDate = DateAdd("d",-fromDiff,fromDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp;toDa te = DateAdd("d",-toDiff,toDate)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 and now find how many weeks, from one Saturday<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 to the other Saturday:<BR>&nbsp;&nbsp;&nbsp;&nbsp;weeks = DateDiff("w",fromDate,toDate)<BR>&nbsp;&nbsp;&nbsp ;&nbsp;&#039 and business days is 5 days per each of those weeks...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 ...adjusted for the two actual starting days<BR>&nbsp;&nbsp;&nbsp;&nbsp;countBizDays = weeks * 5 - fromDiff + toDiff<BR>End Function<BR>%&#062;<BR><BR>&LT;%<BR>&#039 code to test the function:<BR>&#039<BR>startDate = #8/15/2000# &#039 or use a date of your choice<BR>For dnum = 1 To 30 &#039 we will go 30 days out in our testing<BR>&nbsp;&nbsp;&nbsp;&nbsp;endDate = DateAdd("d",dnum,startDate)<BR>&nbsp;&nbsp;&nbsp;& nbsp;Response.Write "There are &LT;B&#062;" & countBizDays(startDate,endDate) _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;& "&LT;/B&#062; business days from " & startDate & " to " & endDate _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;& "&LT;BR&#062;" & vbNewLine<BR>Next<BR>%&#062;<BR><BR>&LT;/BODY&#062;&LT;/HTML&#062;<BR><BR>

#### Posting Permissions

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