473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculate a date, but do not count weekends or holidays

1
So I currently have a set of code that will calculate a date from a start date and will make sure the end date does not land on a weekend or a holiday. Which is great. But I also need it to not count those weekends or holidays when it is adding the number of days to the start date. Here is the function I have now:
Expand|Select|Wrap|Line Numbers
  1. Function fnctAddDate(sPubDate, iIncrement) As String
  2. Dim holidays(19, 11)
  3.     For i = 1 To 19
  4.         If Format$(sPubDate, "yyyy") = holidays(i, 0) Then
  5.             sReturnVolume = i
  6.             i = 19
  7.         End If
  8.     Next
  9.  
  10.  
  11.     bTest = True: nDate = Format(DateAdd("d", iIncrement - 1, sPubDate), "mmmm d, yyyy")
  12.     Do While sDOW = "Sat" Or sDOW = "Sun" Or bTest = True
  13.         nDate = DateAdd("d", 1, nDate)
  14.         sDOW = Format$(nDate, "ddd")
  15.         If sDOW <> "Sat" And sDOW <> "Sun" Then
  16.     bTest = CheckHoliday(sReturnVolume, nDate)
  17.         End If
  18.     Loop
  19.  
  20.     fnctAddDate = nDate
  21.  
  22. End Function
Feb 3 '14 #1
6 7286
Rabbit
12,516 Expert Mod 8TB
Keep a separate count of days added and add one day at a time, checking if it's a holiday or weekend.
Feb 3 '14 #2
ADezii
8,834 Expert 8TB
Here is my own Algorithm that I created to count X number of days after a Start Date, excluding Saturdays, Sundays, and Holidays. I maintain all Holidays in a [Date] Field in a Table named tblHolidays where they are referenced. The Debug.print statement accurately displays the Date Sequence, Day Counter, and Day of the Week. Notice that there are no Saturdays, Sundays, or Valentines Days displayed (Valentines Day is the only Holiday within the Range, namely 20 days from February 1, 2013). I'm sure that you can adapt this to your own specific needs should you so desire.
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcWorkDays2(dteStartDate As Date, lngNumOfDays As Long)
    2. Dim lngCount As Long
    3. Dim lngCtr As Long
    4. Dim dteDate As Date
    5.  
    6. lngCount = 0
    7. lngCtr = 1
    8.  
    9. Debug.Print "Date", "Day Count", "Weekday"
    10.  
    11. Do
    12.   dteDate = DateAdd("d", lngCtr, dteStartDate)
    13.     Select Case Weekday(dteDate)
    14.       Case 7, 1      'Saturday and Sunday, do nothing
    15.       Case Else      'Monday thru Friday, OK
    16.         'Is it a Holiday as posted in tblHolidays?
    17.         If DCount("*", "tblHolidays", "[Date] = #" & dteDate & "#") < 1 Then     'NOT Holiday
    18.           lngCount = lngCount + 1       'Increment Counter
    19.             Debug.Print dteDate, lngCount, Weekday(dteDate)
    20.         End If
    21.     End Select
    22.     lngCtr = lngCtr + 1
    23. Loop While lngCount < lngNumOfDays
    24.   fCalcWorkDays2 = dteDate
    25. End Function
  2. Sample Function Call:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print "--------------------------------------------------"
    2. Debug.Print "20 workdays from 2/1/2014, excluding Holidays is: " & _
    3.              fCalcWorkDays2(#2/1/2014#, 20)
    4. Debug.Print "--------------------------------------------------"
  3. Debug.Print OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. --------------------------------------------------
    2. Date          Day Count     Weekday
    3. 2/3/2014       1             2 
    4. 2/4/2014       2             3 
    5. 2/5/2014       3             4 
    6. 2/6/2014       4             5 
    7. 2/7/2014       5             6 
    8. 2/10/2014      6             2 
    9. 2/11/2014      7             3 
    10. 2/12/2014      8             4 
    11. 2/13/2014      9             5 
    12. 2/17/2014      10            2 
    13. 2/18/2014      11            3 
    14. 2/19/2014      12            4 
    15. 2/20/2014      13            5 
    16. 2/21/2014      14            6 
    17. 2/24/2014      15            2 
    18. 2/25/2014      16            3 
    19. 2/26/2014      17            4 
    20. 2/27/2014      18            5 
    21. 2/28/2014      19            6 
    22. 3/3/2014       20            2 
    23. 20 workdays from 2/1/2014, excluding Holidays is: 3/3/2014
    24. --------------------------------------------------
Feb 4 '14 #3
Hi ADezii,

I use ur Algorithm and my regional setting is non-us date. It work okay for weekend but for holidays it cannot work as my holiday is d/m/yyyy format. Please kindly advice. Thank.
Mar 3 '17 #4
PhilOfWalton
1,430 Expert 1GB
Just to add my ha'p'orth, the following functions give various holiday dates.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function EasterDate(Yr As Integer) As Date
  5.  
  6.     Dim Da As Integer
  7.     Da = (((255 - 11 * (Yr Mod 19)) - 21) Mod 30) + 21
  8.     EasterDate = DateSerial(Yr, 3, 1) + Da + (Da > 48) + 6 - ((Yr + Yr \ 4 + _
  9.     Da + (Da > 48) + 1) Mod 7)
  10.  
  11. End Function
  12.  
  13.  
  14. Public Function GoodFriday(Yr As Integer) As Date
  15.  
  16.     GoodFriday = DateAdd("d", -2, EasterDate(Yr))
  17.  
  18. End Function
  19.  
  20. Public Function EasterMonday(Yr As Integer) As Date
  21.  
  22.     EasterMonday = DateAdd("d", 1, EasterDate(Yr))
  23.  
  24. End Function
  25.  
  26. Public Function AscensionDay(Yr As Integer) As Date
  27.  
  28.     AscensionDay = DateAdd("d", 39, EasterDate(Yr))
  29.  
  30. End Function
  31.  
  32. Public Function FirstSpringHoliday(Yr As Integer) As Date
  33.  
  34.     FirstSpringHoliday = DateSerial(Yr, 5, ((7 - Weekday(DateSerial(Yr, 5, 7))) + 2) Mod 7)
  35.     'DateSerial(Year(mydte), Month(mydte), ((7 - Weekday(DateSerial(Year(mydte), Month(mydte), 7))) + 2) mod 7)
  36.  
  37. End Function
  38.  
  39. Public Function LastSpringHoliday(Yr As Integer) As Date
  40.  
  41.     Dim TheDay As Integer
  42.  
  43.     TheDay = Day(DateSerial(Yr, 5, ((7 - Weekday(DateSerial(Yr, 5, 7))) + 2) Mod 7))     ' First Spring bank holiday
  44.  
  45.     TheDay = TheDay + 28
  46.     If TheDay <= 31 Then
  47.         LastSpringHoliday = DateAdd("d", 28, DateSerial(Yr, 5, ((7 - Weekday(DateSerial(Yr, 5, 7))) + 2) Mod 7))
  48.     Else
  49.         LastSpringHoliday = DateAdd("d", 21, DateSerial(Yr, 5, ((7 - Weekday(DateSerial(Yr, 5, 7))) + 2) Mod 7))
  50.     End If
  51.  
  52. End Function
  53.  
  54. Public Function ChristmasDay(Yr As Integer) As Date
  55.  
  56.     ChristmasDay = DateSerial(Yr, 12, 25)
  57.  
  58. End Function
  59.  
  60. Public Function BoxingDay(Yr As Integer) As Date
  61.  
  62.     BoxingDay = DateSerial(Yr, 12, 26)
  63.  
  64. End Function
  65.  
  66. Public Function NewYearsDay(Yr As Integer) As Date
  67.  
  68.     NewYearsDay = DateSerial(Yr, 1, 1)
  69.  
  70. End Function
  71.  
  72. Public Function Scots2January(Yr As Integer) As Date
  73.  
  74.     Scots2January = DateSerial(Yr, 1, 2)
  75.  
  76. End Function
  77.  
  78. Public Function FirstAugustHoliday(Yr As Integer) As Date
  79.  
  80.     FirstAugustHoliday = DateSerial(Yr, 8, ((7 - Weekday(DateSerial(Yr, 8, 7))) + 2) Mod 7)
  81.     'DateSerial(Year(mydte), Month(mydte), ((7 - Weekday(DateSerial(Year(mydte), Month(mydte), 7))) + 2) mod 7)
  82.  
  83. End Function
  84.  
  85. Public Function LastAugustHoliday(Yr As Integer) As Date
  86.  
  87.     Dim TheDay As Integer
  88.  
  89.     TheDay = Day(DateSerial(Yr, 8, ((7 - Weekday(DateSerial(Yr, 8, 7))) + 2) Mod 7))     ' First Spring bank holiday
  90.  
  91.     TheDay = TheDay + 28
  92.     If TheDay <= 31 Then
  93.         LastAugustHoliday = DateAdd("d", 28, DateSerial(Yr, 8, ((7 - Weekday(DateSerial(Yr, 8, 7))) + 2) Mod 7))
  94.     Else
  95.         LastAugustHoliday = DateAdd("d", 21, DateSerial(Yr, 8, ((7 - Weekday(DateSerial(Yr, 8, 7))) + 2) Mod 7))
  96.     End If
  97.  
  98. End Function
  99.  
  100. Public Function ThanksgivingDay(Yr As Integer) As Date
  101.  
  102.     ThanksgivingDay = DateSerial(Yr, 11, 29 - Weekday(DateSerial(Yr, 11, 1), vbFriday))
  103.  
  104.  End Function
  105.  
  106.  
  107. Public Function DOWsInMonth(Yr As Integer, M As Integer, DOW As Integer) As Integer
  108.  
  109.     '? DOWsInMonth(1998, 5, 2) gives 5
  110.  On Error GoTo DOWsInMonth_Exit
  111.  
  112.     Dim i As Integer
  113.     Dim Lim As Integer
  114.  
  115.     Lim = Day(DateSerial(Yr, M + 1, 0))
  116.  
  117.     DOWsInMonth = 0
  118.     For i = 1 To Lim
  119.         If Weekday(DateSerial(Yr, M, i)) = DOW Then
  120.             DOWsInMonth = DOWsInMonth + 1
  121.         End If
  122.     Next i
  123.     Exit Function
  124.  
  125. DOWsInMonth_Exit:
  126.     DOWsInMonth = 0
  127.  
  128. End Function
  129.  
Phil
Mar 3 '17 #5
Good Evening. This function is exactly what I need for a db that I am building. However, I cannot make it work. It does not return a value when I attempt to use it. I have entered it in a module in Access 2013. Can I ask for more assistance, please?
Jan 26 '18 #6
NeoPa
32,556 Expert Mod 16PB
Only by writing your own question in a separate thread. Not asking it in this one, although a link from and/or to this one is perfectly permissible.

Send me a PM if you need more help using the site.
Jan 27 '18 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rustan | last post by:
Hi Im using GregorianCalendar to find out the current years week numbers. When the user chooses a week number in a dropdown i want to show that week in a table with the corresponding dates. For...
4
by: dhnriverside | last post by:
Hi I have a date in UK Format (dd/mm/yyyy). I want to get a series of days after that date, but NOT Saturday or Sunday For example, if the date is 13/12/2004 (next Monday) and I get the next 9...
4
by: John Devlon | last post by:
Hello everyone... Does anyone know if there is easy way to calculate a certain day in the future ? For example: I would like to calculate when the date is 15 days from today .... I've...
2
by: MLH | last post by:
With a table of holidays and A97's date fn's - how best to count weekends and holidays between two dates? My holiday table has 4 fields. I will be adding records to it each year as info becomes...
0
by: psuaudi | last post by:
I have a database that involves moving product from one status to the next. When the status is changed, it saves the date of the change, and from where the product was moved. Ex. of one product...
1
by: aneinander | last post by:
I'm beginner and getting following error message, please help. The table contains transaction date, account number, branch, etc. And need to show previous transaction date or next transaction date...
8
by: trixxnixon | last post by:
I know this topic is a veritable dead horse, but I have to ask, because I am unable to find something that is close to my scenario that I can completely understand. I have a start date field ...
1
OuTCasT
by: OuTCasT | last post by:
Hi All I need to add 3 months to a date so i can calculate when a clients email bundle is out dated. so for instance the client registed his bundle on 2009/09/01. I need to be able to add 3...
1
by: Irma Solis | last post by:
Hi all how can I calculate date when I only have the week number and year
5
by: Bruce Morris | last post by:
I'm wanting to calculate a date in a field based on a Work Order Priority. Example, return a date from date recieved based on Priority 1, 2 or 3. Priority due in 1 day, 2 due in 15 days 3 due in 30...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.