vb.net - How to calculate holidays for the USA -
i need know how calculate usa holidays. need solution works year. didn't want store dates in database need maintained.
for holidays on weekend needs follow policy of government adjust weekday. if falls on saturday adjusted friday. if falls on sunday needs adjusted monday. understand many (most?) banks in same way.
how calculate list of usa holidays?
public function getholidaylist(byval vyear integer) list(of date) dim holidaylist new list(of date) '...fill list holidays ' new year's day jan 1 ' martin luther king, jr. third mon in jan ' washington's birthday third mon in feb ' memorial day last mon in may ' independence day july 4 ' labor day first mon in sept ' columbus day second mon in oct ' veterans day nov 11 ' thanksgiving day fourth thur in nov ' christmas day dec 25 'adjust weekends end function
this 1 way it. weakness of method since rules hard coded need change code in rare event congress changed rules. in-house software not problem might others.
also don't calculate easter since not federal holiday. see nature (1876) algorithm calculating date of easter
public function getholidaylist(byval vyear integer) list(of date) dim firstweek integer = 1 dim secondweek integer = 2 dim thirdweek integer = 3 dim fourthweek integer = 4 dim lastweek integer = 5 dim holidaylist new list(of date) ' http://www.usa.gov/citizens/holidays.shtml ' http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp ' new year's day jan 1 holidaylist.add(dateserial(vyear, 1, 1)) ' martin luther king, jr. third mon in jan holidaylist.add(getnthdayofnthweek(dateserial(vyear, 1, 1), dayofweek.monday, thirdweek)) ' washington's birthday third mon in feb holidaylist.add(getnthdayofnthweek(dateserial(vyear, 2, 1), dayofweek.monday, thirdweek)) ' memorial day last mon in may holidaylist.add(getnthdayofnthweek(dateserial(vyear, 5, 1), dayofweek.monday, lastweek)) ' independence day july 4 holidaylist.add(dateserial(vyear, 7, 4)) ' labor day first mon in sept holidaylist.add(getnthdayofnthweek(dateserial(vyear, 9, 1), dayofweek.monday, firstweek)) ' columbus day second mon in oct holidaylist.add(getnthdayofnthweek(dateserial(vyear, 10, 1), dayofweek.monday, secondweek)) ' veterans day nov 11 holidaylist.add(dateserial(vyear, 11, 11)) ' thanksgiving day fourth thur in nov holidaylist.add(getnthdayofnthweek(dateserial(vyear, 11, 1), dayofweek.thursday, fourthweek)) ' christmas day dec 25 holidaylist.add(dateserial(vyear, 12, 25)) 'saturday holidays moved fri; sun mon integer = 0 holidaylist.count - 1 dim dt date = holidaylist(i) if dt.dayofweek = dayofweek.saturday holidaylist(i) = dt.adddays(-1) end if if dt.dayofweek = dayofweek.sunday holidaylist(i) = dt.adddays(1) end if next 'return return holidaylist end function private function getnthdayofnthweek(byval dt date, byval dayofweek integer, byval whichweek integer) date 'specify day of week of month , function date 'this function uses month , year of date provided 'get first day of given date dim dtfirst date = dateserial(dt.year, dt.month, 1) 'get first dayofweek of month dim dtret date = dtfirst.adddays(6 - dtfirst.adddays(-(dayofweek + 1)).dayofweek) 'get week dtret = dtret.adddays((whichweek - 1) * 7) 'if day past end of month adjust backwards week if dtret >= dtfirst.addmonths(1) dtret = dtret.adddays(-7) end if 'return return dtret end function
Comments
Post a Comment