# Calculate # Of Business Days

## Recommended Posts

Hey guys.. just wrote a CalcBusinessDays function for someone in the Support forum and figured I would post it here in case someone would ever need that again.

Maybe someday some form of it could get added to Date.au3?

```#include <Date.au3>

Func _CalcBusinessDays(\$Date1, \$Date2); Dates must be in format YYYY/MM/DD
\$TempDate = \$Date1
While \$TempDate <> \$Date2
\$TempDateArray = StringSplit(\$TempDate, "/")
\$TempDateArray[1] = Number(\$TempDateArray[1])
\$TempDateArray[2] = Number(\$TempDateArray[2])
\$TempDateArray[3] = Number(\$TempDateArray[3])
\$DayOfWeek = _DateToDayOfWeek(\$TempDateArray[1],\$TempDateArray[2],\$TempDateArray[3])
If \$DayOfWeek = 1 Or \$DayOfWeek = 7 Then
Else
\$r = \$r + 1
EndIf
WEnd
Return \$r
EndFunc```

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

##### Share on other sites

Couple of issues with your UDF:

\$R is not defined this gives and error.

When the \$Date1 is greater than \$Date2 the UDF will loop indefinitely.

When the dates are far apart, the UDF will be very slow, This can be solve by calculating the numebr of full weeks and only use your logic for the remaining days..

Here is more or less what i mean:

```Func _CalcBusinessDays(\$Date1, \$Date2); Dates must be in format YYYY/MM/DD
Local \$TempDate,\$dummy
Local \$Days = Abs(_DateDiff("d",\$Date1, \$Date2))
ConsoleWrite('@@ Debug(27) : \$Days = ' & \$Days & @lf & '>Error code: ' & @error & @lf);### Debug Console
Local \$Weeks = Abs(_DateDiff("w",\$Date1, \$Date2))
Local \$Rest = \$Days - (\$Weeks * 7)
Local \$BDays = \$Weeks * 5
For \$x = 1 To \$rest
\$DayOfWeek = _DateToDayOfWeek(\$TempDate[1],\$TempDate[2],\$TempDate[3])
If Not (\$DayOfWeek = 1 Or \$DayOfWeek = 7) Then
\$BDays = \$BDays + 1
EndIf
Next
Return \$BDays
EndFunc```

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Excellent JdeB. I'm still very new at AutoIt.. and it is great to see how I could do things in a more refined manner. I just saw the \$r variable not being declared, I had taken it out of the post that I made earlier. Thanks for pointing this out.

Thanks again,

Simucal

Edited by Simucal

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

## Create an account

Register a new account