473,761 Members | 8,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I carve out holidays from date calculations?

2 New Member
Hello. I am using the below code courtesy of The Smiley Coder which works beautifully. I would now like to carve out holidays in addition to the weekends. I believe I will have to create a table of the holidays, but I'm not certain how to work it into the code. Any assistance is greatly appreciated.

Public Function addWorkDays(Dat eInput As Date, intDays As Integer) As Date
'Function that only adds days if they are workdays (monday-friday)
'Holidays are not excluded.

'Setup a working date
Dim myDT As Date
myDT = DateInput

'Setup number of days remaining to add
Dim intDaysRemain As Integer
intDaysRemain = intDays

'Now add days 1 at a time,
' while counting down intDaysRemain,
' but only if day is a working day
Do Until intDaysRemain = 0
myDT = DateAdd("d", 1, myDT)
Select Case Weekday(myDT)
Case vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday
intDaysRemain = intDaysRemain - 1
Case vbSaturday, vbSunday
'Don't modify intDaysRemain
End Select
Loop
'Return answer
addWorkDays = myDT

End Function
Mar 28 '11 #1
2 1913
gershwyn
122 New Member
I think the simplest way to do that, given your existing code, is to create a table of holidays that your organization observes. We then just need to add a check to see if any given date is in that table.

I created a table called HolidayTable with two fields: HolidayName and HolidayDate, and filled it with a couple entries:
Expand|Select|Wrap|Line Numbers
  1. HolidayName   HolidayDate
  2. New Year's    01/01/2011
  3. Memorial Day  05/30/2011
  4. 4th of July   07/04/2011
  5.  
Then add a couple lines to your code (added lines in bold):
Expand|Select|Wrap|Line Numbers
  1. Select Case Weekday(myDT)
  2.   Case vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday
  3.     'Check to see if holiday
  4.     If DCount(1, "HolidayTable", "HolidayDate = #" & myDT & "#") < 1 Then
  5.       intDaysRemain = intDaysRemain - 1
  6.     End If
  7.   Case vbSaturday, vbSunday
  8.     'Don't modify intDaysRemain
  9. End Select
  10.  
This should now ignore any dates that are in your table.
Expand|Select|Wrap|Line Numbers
  1. ?addWorkDays(#5/29/2011#, 7)
  2. 6/8/2011 
  3.  
  4. ?addWorkDays(#5/30/2011#, 7)
  5. 6/8/2011 
  6.  
  7. ?addWorkDays(#5/31/2011#, 7)
  8. 6/9/2011 
  9.  
Mar 29 '11 #2
Stephanie Moll
2 New Member
Your code works perfectly! Thank you so much.
Mar 29 '11 #3

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

Similar topics

1
2078
by: mene | last post by:
I have a field that contains date information, and sometimes time information as well. I would like to be able to take that date and do a calculation on it. Here are some examples of what is in the field: 01/12/2003 5:04:00 PM 24/11/2003 19/05/2003 6:30:00 AM How can I take that date, then do a calculation like minus 5 days from the date. I understand that I am to use the GETDATE() function, but below is
11
4675
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
2
7334
by: Joe Jax | last post by:
I have some date calculations that add a time span to a date. The problem is, when I add a time span that is a whole number of days to a date, the result can be +/- 1 hour due to daylight savings. Is there anyway to disable this?
2
425
by: Mark Lees | last post by:
I need some help setting up some date caluclations. Fields Include: A. DOB "date of birth" B. IFSPOrginal "Date IFSP is entered" C. IFSPRedo "Date 12 months into the future from IFSPOrginal" D. 2yrTransistion "Date 2yrs into the futre from DOB" I'm having difficulty creating the expressions for the (C)IFSPRedo and
2
404
by: Roffee | last post by:
Hi. I'm a bit confused about date functions in Access. I need the program to calculate a new date that lays 15 days ahead of a given date in a table. I studied the datediff function, but as I see it, it only returns a number of days (or other time period) between two dates. I need a future date as output. Hints anyone?
2
6516
by: Julie Wardlow | last post by:
Help! I am calculating a future date using the DateAdd function in a query (the calculation also involves an IIf statement), and have managed to get this formula to produce the required result. I then want to search through the records and select those with dates (as caluclated above) within a user defined range, and so I am using a parameter query. However, this query returns dates outside of the range and appears to have particular...
1
6134
by: Wayne | last post by:
Hi all I'm trying to calculate the number of days (or workdays) between 2 given dates that do not include weekend days or public holidays (public holidays are user defined from a dbase, have a start date & an end date & may span a weekend) If a start date (workday) & an end date (workday) are on the same day then the number of workdays will equal zero Some eg's.
5
1909
by: Miguel Arenas | last post by:
In VB6.0 I Did This operation with dates. Dim d as String D=Date D=D-1 or D=D+1 In Vb.2003 Dim d As String
8
2112
by: Charlie Brookhart | last post by:
I am creating a program that involves having to find the difference between two dates and converting it to a number to be used for calculations. The problem is that the way it is setup, VB is not doing anything with the dates that I have selected. I hope that someone here can help me figure out why it is not working what I have to do to make it work. This the code I have for the date calculations. txtNumberOfDays.text =...
11
3919
by: arggg | last post by:
I have a table with with Last_Modified field as VARCHAR(55) which stores dates in the format of 2/15/08 1:34 PM. What I need to do is query the database and select ALL rows that have the Last_Modified field >= 6 days from the current date/time. It appears I will need to convert the dates so I can use the mysql calculations but I'm unsure where to begin on this.
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.