Link to home
Start Free TrialLog in
Avatar of Sara_j_11
Sara_j_11

asked on

Visual Basic - holiday adjustment

I need only for the following holidays:
new year  - jan 01
christmas - dec 25
thanksgiving - last thursday
labor day
memorial day

I have a VB 6.0 program that reads a sql (oracle) and exceutes it and then puts the results in a csv file.
Now I want to send the value to a parameter in my sql code from vb code(I have a parameter called &x in the where clause).
The parameter will be 42 if there are no holidays during the 42 days.(which is nothing but 30 business days) But if there were any publice holidays then the the value of the parametr changes to (42 -1) if there was just one holiday or
(42-2) if there were 2 holidays.

I have found the alogorithm that computes holidays, but I am getting some errors.
So pl. update the changes in my code for this:
Algo:
To calculate the day on which a particular date falls, the following
algorithm may be used (the divisions are integer divisions, in which
remainders are discarded; % means all we want is the remainder):
 
a = (14 - month) / 12
y = year - a
m = month + 12*a - 2
For Julian calendar: d = (5 + day + y + y/4 + (31*m)/12) % 7
For Gregorian calendar: d = (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7
 
The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

Then I sat down and came up with this formula in order to calculate dates such as "The third Monday in January". I suspect these have been derived and written down somewhere by someone else; in any case, these formula are easy to derive, and useful for computing various holidays in electronic calendars.
First, let the above formula be called DoW(year,month,dayinmonth), which specifies that its arguements are the year (in numerical form), the month (1-12) and the day in the month (day number in month, 1-31).
In all the below formula, the following common-sense relation is used: -1%7 = 6; -2%7=5; .. -6%7=1, -7%7=0. Also, an N-day is a Sunday (N=0), through Saturday (N=6). The most generic formula is then:
Date In Month that is an N-day ON OR AFTER date Year-Month-Day =
Day + (N - DoW(Year,Month,Day))%7 .

Date In Month that is an N-day ON OR BEFORE date Year-Month-Day =
Day - (DoW(Year,Month,Day) - N)%7 .


These lead to quick formulae for determining the date of the first, second, third, fourth and fifth occurence of a Sunday, Monday, etc., in any particular month:
First N-day: N1 = 1 + (N - DoW(Year,Month,1))%7 ;
2nd N-day : N2 = 8 + (N - DoW(Year,Month,8))%7 ;
3rd N-day : N3 = 15 + (N - DoW(Year,Month,15))%7 ;
4th N-day : N4 = 22 + (N - DoW(Year,Month,22))%7 ;
5th N-day : N5 = 29 + (N - DoW(Year,Month,29))%7 .
(Note: Use common sense when trying to calculate the fifth N-day: check to see if the value you obtain is greater than the number of days in the month; if it is, the there is no fifth N-day in that month.)


Two visitors to this page, Timothy Barmann and Bobby Cossum, have independently suggested that the above five equations can be simplified into just one equation. Let Q be the occurence (first, second, third, fourth, fifth), and N will still represent the day of the week, as above. Then,
the Q-th N-day: NQ = 1 + (Q-1)*7 + (N - DoW(Year,Month,1))%7;
or equivalently
the Q-th N-day: NQ = 7*Q - 6 + (N - DoW(Year,Month,1))%7. So, to find the first Friday using the above equations, use Q=1, N=5; the third Monday is found using Q=3, N=1, etc.
In order to find, for example, the LAST Monday in a month, we need to know the length of the month; for all months except February, this is, of course, fixed. In any case, we have:
ND=Number of last day in month;
Last N-Day : NL = ND - (DoW(Year,Month,ND) - N)%7 .
Example: What date is the last Monday in May, 1996?
1. The last day in May is May 31, so ND=31.
2. Monday is what we want, so N=1
3. The day of the week of May 31, 1996 is found by following the first algorithm above: a=(14-5)/12=0
y=1996-a=1996-0=1996
m=5+0-2=3
d=(31+1996+499-19+4+(31*3)/12)%7= 5
So, May 31st is a Friday; then
4. NL=31-(5-1)%7=31-4=27
5. So, the last Monday in 1996 May is May 27.

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Not sure what your error is.

Perhaps you should post your code, indicating where and how it fails.
In the dissertation that you post, the authors seem to be unaware that the number of the day of the week for a given date is simply deriven in VB:

Weekday(GivenDate)
Avatar of leojl
leojl


hello

My neighbor has a bible type book which shows the date for Easter for about 400
years. Easter is not on your list, but I use that as an example of the most simple
cost effective way to deal with your requirement. Just build a file or table with your
dates of interest. Then address that table with start and end dates of interest and
count the date tick marks.

It is perhaps more interesting to create a formula or general algorithm, but some
requirements are much better met with a simpe hard-wired table.

leojl
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 I need only for the following holidays:
new year  - jan 01
christmas - dec 25
thanksgiving - last thursday
labor day
memorial day

how long would it take to build a table for 10, 20 or 50 years ??

I just want EE people to consider that sometimes a brute force technique
is the most cost effective way to deal with a requirement.

leo