0

I need to determine whether or not the next two consecutive working days are holidays (US working days), excluding weekends.

My efforts to solve this:

todayVariable = Date.Now()

Taking the datepart of this variable, if it is 1 or 7, that means it's a weekend, so I will leave it alone. However, what I don't know is how to determine if the week days are not holidays. How can I get a list of, for instance, all the official holidays for the next two years?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
user1733138
  • 9
  • 1
  • 1
  • 4
  • any language preferably vb.net or sql query – user1733138 Oct 12 '12 at 19:01
  • 2
    How do you plan to look up what a holiday is? If you had some sort of List Holidays, you could just check if the next two days are a holiday. This post may be helpful in that regard. http://stackoverflow.com/questions/457176/how-do-i-determine-a-public-holiday-in-sql-server – Corey Oct 12 '12 at 19:06
  • I am going to gather List Holidays ( us holidays). How to gather them..atleast for next two years list is enough for time being. – user1733138 Oct 12 '12 at 19:23
  • Do you want an answer in SQL or in VB? – Jamie F Oct 12 '12 at 21:09

3 Answers3

3

Use this to answer if tomorrow is a holiday:

Dim todayVariable As Date = Today
Debug.Print(isHoliday(todayVariable.AddDays(1)))

Function isHoliday(ByVal dt As Date)
    Return getHolidayList(dt.Year).Any(Function(x) x = dt)
End Function

The code below will calculate the dates for US Federal Holidays. A weakness of this method is that since the rules are hard coded I would need to change the code in the rare event that congress changed the rules. For my in-house software that is not a problem but it might be for others.

Also I don't calculate Easter since that is not a US federal holiday. See Nature (1876) Algorithm for Calculating the Date of Easter

Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)

    Dim FirstWeek As Integer = 1
    Dim SecondWeek As Integer = 2
    Dim ThirdWeek As Integer = 3
    Dim FourthWeek As Integer = 4
    Dim LastWeek As Integer = 5

    Dim HolidayList As 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 are moved to Fri; Sun to Mon
    For i As Integer = 0 To HolidayList.Count - 1
        Dim dt As Date = HolidayList(i)
        If dt.DayOfWeek = DayOfWeek.Saturday Then
            HolidayList(i) = dt.AddDays(-1)
        End If
        If dt.DayOfWeek = DayOfWeek.Sunday Then
            HolidayList(i) = dt.AddDays(1)
        End If
    Next

    'return
    Return HolidayList

End Function

Private Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided

    'get first day of the given date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

    'get first DayOfWeek of the month
    Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

    'get which week
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

    'if day is past end of month then adjust backwards a week
    If dtRet >= dtFirst.AddMonths(1) Then
        dtRet = dtRet.AddDays(-7)
    End If

    'return
    Return dtRet

End Function
D_Bester
  • 5,723
  • 5
  • 35
  • 77
0

There isn't a commonly agreed upon list of official U.S. holidays. Probably the closest thing is the federal holiday calendar, but that includes days that most businesses remain open, such as Washington's Birthday and Columbus Day.

Many businesses opt to put their holidays in a database table or combine into a calendar table.

Jamie F
  • 23,189
  • 5
  • 61
  • 77
  • Yes I do have a table which consis fields holidayID, holidayDate, Date Title; 95 2011-04-22 Good friday ( one record) – user1733138 Oct 12 '12 at 21:03
  • Are you sure that "There isn't a commonly agreed upon list of official U.S. holidays."? I can't believe that. – Krisztián Balla Jan 08 '14 at 11:54
  • There is a list of Federal holidays but many of those are not observed by many businesses: The third Monday in February celebrates Washington's birthday, USMail won't be delivered, but I will be at work, as will most of my customers. FedEx and UPS will operate as usual. So as the OP was asking, I don't know how you'd determine working days for any business without knowing their policies. – Jamie F Jan 08 '14 at 15:08
0

As others have pointed out already, there doesn't seem to be a list of holidays that you can "compute" or simply "get-somewhere". You'll have to hard-code the applicable list of dates.

A few remarks on your description, to help you avoid any other issues:

Date.Now gives the Date AND the Time of "now". You may use Date.Today if you wish to compare dates without regarding times.

dateValue.DatePart gives you, indeed, the Date-part (Year, Month, Day). You presumably want to know the day-of-week (Sunday, Monday, ...). This goes as:

  Dim dayValue As System.DayOfWeek = dateValue.DayOfWeek

The result is an integer Enum with values Sunday(=0), Monday(=1) etcetera.

To avoid confusion: the Day property of a dateValue (dateValue.Day) gives you the day-number in a month (1..31).

Bert te Velde
  • 823
  • 5
  • 8