Calculate work (working / business) days in period
Below method can be used in order to calculate work (working / business) days in VBScript ignoring bank holidays.
Version History
Version
Date
Description
0.1
2014-11-12
Initial commit.
0.2
2015-05-20
Replaced the old algorithm with a smarter solution, which is also regarding the fact that the start date can be younger than an end date.
Source Code
The old version failed in calculating the difference in days between dates if the start date was younger than the end date of a period.
Furthermore this solution is expected to be slightly faster and it is more elegant.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
' @Author - Alexander Bolte' @ChangeDate - 2015-05-20' @Description - calculates the difference of whole work days' between provided dates.' @Param dtStart - a Date providing the start date of a period.' @Param dtEnd - a Date providing the end date of a period.' @Returns an Integer holding the difference in work days in defined period.FunctiongetWorkDaysDiff(dtStart,dtEnd)Dimweeks' As IntegerDimweekDays' As IntegerDimlastDays' As IntegerDimfirstDays' As IntegerDimworkDays' As IntegerIfdtStart<=dtEndThen' Calculate the difference of weeks and sbstract one' because we have to exclude the week of a handed start date.weeks=DateDiff("ww",dtStart,dtEnd)-1' Calculate the number of work days in whole weeks.weekDays=weeks*5IfWeekday(dtStart)=vbSaturdayThenfirstDays=0ElsefirstDays=7-Weekday(dtStart)-1EndIfIfWeekday(dtEnd)=vbSaturdayThenlastDays=Weekday(dtEnd)-2ElselastDays=Weekday(dtEnd)-1EndIfworkDays=firstDays+lastDays+weekDaysElse' Call the method recursively in order to avoid having to code around the' comparisons above into the negative direction.workDays=getWorkDaysDiff(dtEnd,dtStart)*-1EndIfgetWorkDaysDiff=workDaysEndFunction
The original algorithm can be found here, but I think it also does leave a calling method with having to decide what date to hand as start date.
' @Author - Alexander Bolte' @ChangeDate - 2014-11-12' @Description - calculating and returning the difference of whole work days' between provided dates.' @Param dtStart - a Date providing the start date of viewed period.' @Param dtEnd - a Date providing the end date of a viewed period.' @Returns an Integer holding the difference in work days in defined period.FunctiongetWorkDaysDiff(dtStart,dtEnd)DimtotalDays' As IntegerDimtotalWeeks' As IntegerDimdaysFirstWeek' As IntegerDimdaysLastWeek' As IntegerDimmodDays' As IntegertotalDays=DateDiff("D",dtStart,dtEnd)totalWeeks=DateDiff("W",dtStart,dtEnd)'COUNT DAYS IN FIRST WEEK -WEEKENDSSelectCaseWeekDay(dtStart)CasevbMondaydaysFirstWeek=5CasevbTuesdaydaysFirstWeek=4CasevbWednesdaydaysFirstWeek=3CasevbThursdaydaysFirstWeek=2CasevbFridaydaysFirstWeek=1CasevbSaturdaydaysFirstWeek=0CasevbSundaydaysFirstWeek=0EndSelect'COUNT DAYS IN END WEEK -WEEKENDSSelectCaseWeekDay(dtEnd)CasevbSaturdaydaysLastWeek=0CasevbSundaydaysLastWeek=0CasevbMondaydaysLastWeek=0CasevbTuesdaydaysLastWeek=1CasevbWednesdaydaysLastWeek=2CasevbThursdaydaysLastWeek=3CasevbFridaydaysLastWeek=4EndSelectmodDays=totalDaysMod7'GET DAYS OUTSIDE OF WEEKSifmodDays<daysFirstWeek+daysLastWeekthenoddDays=modDayselseoddDays=daysFirstWeek+daysLastWeekendif'modDays < daysFirstWeek + daysLastWeekiftotalDays<daysFirstWeekthenwrkDays=totalDays+(totalWeeks*5)elsewrkDays=oddDays+(totalWeeks*5)endif'totalDays < daysFirstWeekgetWorkDaysDiff=wrkDaysEndFunction