Calculate Holidays-VBForums
Results 1 to 8 of 8

Thread: Calculate Holidays

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,038

    Calculate Holidays

    A way to generate Holidays. Several are defined in the Class:

    Code:
        Public Class MyHolidays
    
            Private Class DefHolidays
                Property Name As String
                Property HolidayMonth As Integer = -1
                Property HolidayDayOfMonth As Integer = -1
                Property HolidayDate As DateTime
                Property Nth As Integer = -1 '-1 last NthDayOfWeek <<<<<<<<<<
                Property NthDayOfWeek As DayOfWeek
                Property EasterCalc As Boolean = False
                Property enabled As Boolean = True
            End Class
    
            Private holidays As New Dictionary(Of String, DefHolidays)
            Private whYear As Integer
            Public Easter As DateTime = DateTime.MinValue
    
            ''' <summary>
            ''' creates holidays for the given year
            ''' </summary>
            ''' <param name="year"></param>
            ''' <remarks></remarks>
            Public Sub New(year As Integer)
                Me.whYear = year
    
                Dim holiday As DefHolidays
    
                'what follows are the holidays
                'add / remove / disable per your requirement
    
                holiday = New DefHolidays
                With holiday
                    .Name = "New Year's Day"
                    .HolidayMonth = 1
                    .HolidayDayOfMonth = 1
                    '.enabled = False 'how to disable a holiday
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Martin Luther King Jr. Day" 'third monday january
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 1
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Groundhog Day"
                    .HolidayMonth = 2
                    .HolidayDayOfMonth = 2
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Valentine's Day"
                    .HolidayMonth = 2
                    .HolidayDayOfMonth = 14
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Presidents Day" 'second monday of Feb.
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 2
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "St. Patrick's Day"
                    .HolidayMonth = 3
                    .HolidayDayOfMonth = 17
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "April Fools' Day"
                    .HolidayMonth = 4
                    .HolidayDayOfMonth = 1
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                'easter defined before holidays based on easter
                holiday = New DefHolidays
                With holiday
                    .Name = "Easter Sunday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Palm Sunday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Good Friday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Patriot's Day"
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Earth Day"
                    .HolidayMonth = 4
                    .HolidayDayOfMonth = 22
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Arbor Day" 'last fri in apr ,  .Nth = -1 (the default)
                    .NthDayOfWeek = DayOfWeek.Friday
                    .HolidayMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Mother's Day"
                    .Nth = 2
                    .NthDayOfWeek = DayOfWeek.Sunday
                    .HolidayMonth = 5
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Memorial Day"
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 5
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Flag Day"
                    .HolidayMonth = 6
                    .HolidayDayOfMonth = 14
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Father's Day"
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Sunday
                    .HolidayMonth = 6
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Independence Day"
                    .HolidayMonth = 7
                    .HolidayDayOfMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Labor Day"
                    .Nth = 1
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 9
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Columbus Day"
                    .Nth = 2
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 10
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Halloween"
                    .HolidayMonth = 10
                    .HolidayDayOfMonth = 31
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Veterans Day"
                    .HolidayMonth = 11
                    .HolidayDayOfMonth = 11
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Thanksgiving"
                    .Nth = 4
                    .NthDayOfWeek = DayOfWeek.Thursday
                    .HolidayMonth = 11
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Pearl Harbor Remembrance Day"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 7
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Christmas Eve"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 24
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Christmas Day"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 25
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "New Year's Eve"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 31
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
            End Sub
    
            Private Function CalcDate(holiday As DefHolidays) As DateTime
                If holiday.EasterCalc Then
                    If holiday.Name.StartsWith("Easter") Then
                        'easter
                        'VB .Net implementation of:
                        'http://aa.usno.navy.mil/faq/docs/easter.php
                        Dim y As Integer = Me.whYear
                        Dim c, d, i, j, k, l, m, n As Integer
                        c = y \ 100
                        n = y Mod 19
                        k = (c - 17) \ 25
                        i = c - c \ 4 - (c - k) \ 3 + 19 * n + 15
                        i = i Mod 30
                        i = i - (i \ 28) * (1 - (i \ 28) * (29 \ (i + 1)) * ((21 - n) \ 11))
                        j = y + y \ 4 + i + 2 - c + c \ 4
                        j = j Mod 7
                        l = i - j
                        m = 3 + (l + 40) \ 44
                        d = l + 28 - 31 * (m \ 4)
                        holiday.HolidayMonth = m
                        holiday.HolidayDayOfMonth = d
                        Me.Easter = New DateTime(y, m, d)
    
                    ElseIf holiday.Name.Contains(DayOfWeek.Friday.ToString) Then
                        'good friday - friday before easter sunday
                        holiday.HolidayMonth = Me.Easter.AddDays(-2).Month
                        holiday.HolidayDayOfMonth = Me.Easter.AddDays(-2).Day
    
                    ElseIf holiday.Name.Contains(DayOfWeek.Sunday.ToString) Then
                        'palm sunday - sunday before easter
                        holiday.HolidayMonth = Me.Easter.AddDays(-7).Month
                        holiday.HolidayDayOfMonth = Me.Easter.AddDays(-7).Day
                    End If
    
                ElseIf holiday.HolidayMonth = -1 OrElse holiday.HolidayDayOfMonth = -1 Then
                    If holiday.Nth < 1 Then
                        'last NthDayOfWeek
                        Dim dt As DateTime = New DateTime(Me.whYear, holiday.HolidayMonth, DateTime.DaysInMonth(Me.whYear, holiday.HolidayMonth))
                        Do While dt.DayOfWeek <> holiday.NthDayOfWeek
                            dt = dt.AddDays(-1)
                        Loop
                        holiday.HolidayDayOfMonth = dt.Day
                    Else
                        Dim dt As DateTime = New DateTime(Me.whYear, holiday.HolidayMonth, 1)
                        For x As Integer = 1 To holiday.Nth
                            Do While dt.DayOfWeek <> holiday.NthDayOfWeek
                                dt = dt.AddDays(1)
                            Loop
                            dt = dt.AddDays(1)
                        Next
                        dt = dt.AddDays(-1)
                        holiday.HolidayDayOfMonth = dt.Day
                    End If
                End If
                holiday.HolidayDate = New DateTime(Me.whYear, holiday.HolidayMonth, holiday.HolidayDayOfMonth)
                Return holiday.HolidayDate
            End Function
    
            ''' <summary>
            ''' returns a list of Holidays
            ''' </summary>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function HolidayList() As List(Of String)
                Dim rv As New List(Of String)
                For Each h As DefHolidays In Me.holidays.Values.OrderBy(Function(d) d.HolidayDate)
                    If h.enabled Then
                        rv.Add(String.Format("{0},  {1},  {2}", h.Name, h.HolidayDate.DayOfWeek, h.HolidayDate.ToString("MMMM d, yyyy")))
                    End If
                Next
                Return rv
            End Function
    
            ''' <summary>
            ''' check if date is a holiday
            ''' </summary>
            ''' <param name="SomeDateInYear">a date.  year must match year for list of holidays</param>
            ''' <returns>true if is a holiday, otherwise false</returns>
            ''' <remarks></remarks>
            Public Function isHoliday(SomeDateInYear As DateTime) As Boolean
                If SomeDateInYear.Year <> Me.whYear Then
                    Throw New ArgumentException("These holidays are for " & Me.whYear.ToString)
                Else
                    Dim ct As Integer = (From d In Me.holidays.Values
                                        Where d.HolidayDate.Date = SomeDateInYear.Date AndAlso d.enabled
                                        Select d Take 1).Count
                    Return ct > 0
                End If
            End Function
    
        End Class
    Last edited by dbasnett; Nov 18th, 2014 at 04:46 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,284

    Re: Holidays - USA

    Nice code however, you might want to add comments to the code to say what the variables represent c, n, k, etc me nothing to me and I am sure some others feel the same way. Also, you might want to change the thread title to something such as "How to find US holidays through code". After all, I don't want anybody confusing it for a chit chat thread.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,038

    Re: Holidays - USA

    The Easter calculation was from the location specified. Sadly, other than y,m,d, I haven't a clue what the other variables are or what each line of code does. I do know that the whole calculates Easter accurately.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  4. #4
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,863

    Re: Calculate Holidays

    Quote Originally Posted by dbasnett View Post
    A way to generate Holidays. Several are defined in the Class:

    Code:
        Public Class MyHolidays
    
            Private Class DefHolidays
                Property Name As String
                Property HolidayMonth As Integer = -1
                Property HolidayDayOfMonth As Integer = -1
                Property HolidayDate As DateTime
                Property Nth As Integer = -1 '-1 last NthDayOfWeek <<<<<<<<<<
                Property NthDayOfWeek As DayOfWeek
                Property EasterCalc As Boolean = False
                Property enabled As Boolean = True
            End Class
    
            Private holidays As New Dictionary(Of String, DefHolidays)
            Private whYear As Integer
            Public Easter As DateTime = DateTime.MinValue
    
            ''' <summary>
            ''' creates holidays for the given year
            ''' </summary>
            ''' <param name="year"></param>
            ''' <remarks></remarks>
            Public Sub New(year As Integer)
                Me.whYear = year
    
                Dim holiday As DefHolidays
    
                'what follows are the holidays
                'add / remove / disable per your requirement
    
                holiday = New DefHolidays
                With holiday
                    .Name = "New Year's Day"
                    .HolidayMonth = 1
                    .HolidayDayOfMonth = 1
                    '.enabled = False 'how to disable a holiday
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Martin Luther King Jr. Day" 'third monday january
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 1
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Groundhog Day"
                    .HolidayMonth = 2
                    .HolidayDayOfMonth = 2
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Valentine's Day"
                    .HolidayMonth = 2
                    .HolidayDayOfMonth = 14
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Presidents Day" 'second monday of Feb.
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 2
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "St. Patrick's Day"
                    .HolidayMonth = 3
                    .HolidayDayOfMonth = 17
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "April Fools' Day"
                    .HolidayMonth = 4
                    .HolidayDayOfMonth = 1
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                'easter defined before holidays based on easter
                holiday = New DefHolidays
                With holiday
                    .Name = "Easter Sunday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Palm Sunday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Good Friday"
                    .EasterCalc = True
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Patriot's Day"
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Earth Day"
                    .HolidayMonth = 4
                    .HolidayDayOfMonth = 22
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Arbor Day" 'last fri in apr ,  .Nth = -1 (the default)
                    .NthDayOfWeek = DayOfWeek.Friday
                    .HolidayMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Mother's Day"
                    .Nth = 2
                    .NthDayOfWeek = DayOfWeek.Sunday
                    .HolidayMonth = 5
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Memorial Day"
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 5
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Flag Day"
                    .HolidayMonth = 6
                    .HolidayDayOfMonth = 14
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Father's Day"
                    .Nth = 3
                    .NthDayOfWeek = DayOfWeek.Sunday
                    .HolidayMonth = 6
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Independence Day"
                    .HolidayMonth = 7
                    .HolidayDayOfMonth = 4
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Labor Day"
                    .Nth = 1
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 9
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Columbus Day"
                    .Nth = 2
                    .NthDayOfWeek = DayOfWeek.Monday
                    .HolidayMonth = 10
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Halloween"
                    .HolidayMonth = 10
                    .HolidayDayOfMonth = 31
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Veterans Day"
                    .HolidayMonth = 11
                    .HolidayDayOfMonth = 11
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Thanksgiving"
                    .Nth = 4
                    .NthDayOfWeek = DayOfWeek.Thursday
                    .HolidayMonth = 11
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Pearl Harbor Remembrance Day"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 7
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Christmas Eve"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 24
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "Christmas Day"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 25
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
                holiday = New DefHolidays
                With holiday
                    .Name = "New Year's Eve"
                    .HolidayMonth = 12
                    .HolidayDayOfMonth = 31
                End With
                Me.holidays.Add(holiday.Name, holiday)
                Me.CalcDate(holiday)
    
            End Sub
    
            Private Function CalcDate(holiday As DefHolidays) As DateTime
                If holiday.EasterCalc Then
                    If holiday.Name.StartsWith("Easter") Then
                        'easter
                        'VB .Net implementation of:
                        'http://aa.usno.navy.mil/faq/docs/easter.php
                        Dim y As Integer = Me.whYear
                        Dim c, d, i, j, k, l, m, n As Integer
                        c = y \ 100
                        n = y Mod 19
                        k = (c - 17) \ 25
                        i = c - c \ 4 - (c - k) \ 3 + 19 * n + 15
                        i = i Mod 30
                        i = i - (i \ 28) * (1 - (i \ 28) * (29 \ (i + 1)) * ((21 - n) \ 11))
                        j = y + y \ 4 + i + 2 - c + c \ 4
                        j = j Mod 7
                        l = i - j
                        m = 3 + (l + 40) \ 44
                        d = l + 28 - 31 * (m \ 4)
                        holiday.HolidayMonth = m
                        holiday.HolidayDayOfMonth = d
                        Me.Easter = New DateTime(y, m, d)
    
                    ElseIf holiday.Name.Contains(DayOfWeek.Friday.ToString) Then
                        'good friday - friday before easter sunday
                        holiday.HolidayMonth = Me.Easter.AddDays(-2).Month
                        holiday.HolidayDayOfMonth = Me.Easter.AddDays(-2).Day
    
                    ElseIf holiday.Name.Contains(DayOfWeek.Sunday.ToString) Then
                        'palm sunday - sunday before easter
                        holiday.HolidayMonth = Me.Easter.AddDays(-7).Month
                        holiday.HolidayDayOfMonth = Me.Easter.AddDays(-7).Day
                    End If
    
                ElseIf holiday.HolidayMonth = -1 OrElse holiday.HolidayDayOfMonth = -1 Then
                    If holiday.Nth < 1 Then
                        'last NthDayOfWeek
                        Dim dt As DateTime = New DateTime(Me.whYear, holiday.HolidayMonth, DateTime.DaysInMonth(Me.whYear, holiday.HolidayMonth))
                        Do While dt.DayOfWeek <> holiday.NthDayOfWeek
                            dt = dt.AddDays(-1)
                        Loop
                        holiday.HolidayDayOfMonth = dt.Day
                    Else
                        Dim dt As DateTime = New DateTime(Me.whYear, holiday.HolidayMonth, 1)
                        For x As Integer = 1 To holiday.Nth
                            Do While dt.DayOfWeek <> holiday.NthDayOfWeek
                                dt = dt.AddDays(1)
                            Loop
                            dt = dt.AddDays(1)
                        Next
                        dt = dt.AddDays(-1)
                        holiday.HolidayDayOfMonth = dt.Day
                    End If
                End If
                holiday.HolidayDate = New DateTime(Me.whYear, holiday.HolidayMonth, holiday.HolidayDayOfMonth)
                Return holiday.HolidayDate
            End Function
    
            ''' <summary>
            ''' returns a list of Holidays
            ''' </summary>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function HolidayList() As List(Of String)
                Dim rv As New List(Of String)
                For Each h As DefHolidays In Me.holidays.Values.OrderBy(Function(d) d.HolidayDate)
                    If h.enabled Then
                        rv.Add(String.Format("{0},  {1},  {2}", h.Name, h.HolidayDate.DayOfWeek, h.HolidayDate.ToString("MMMM d, yyyy")))
                    End If
                Next
                Return rv
            End Function
    
            ''' <summary>
            ''' check if date is a holiday
            ''' </summary>
            ''' <param name="SomeDateInYear">a date.  year must match year for list of holidays</param>
            ''' <returns>true if is a holiday, otherwise false</returns>
            ''' <remarks></remarks>
            Public Function isHoliday(SomeDateInYear As DateTime) As Boolean
                If SomeDateInYear.Year <> Me.whYear Then
                    Throw New ArgumentException("These holidays are for " & Me.whYear.ToString)
                Else
                    Dim ct As Integer = (From d In Me.holidays.Values
                                        Where d.HolidayDate.Date = SomeDateInYear.Date AndAlso d.enabled
                                        Select d Take 1).Count
                    Return ct > 0
                End If
            End Function
    
        End Class
    What version of VB is this written in?
    When I paste the code into VB 2005, I am told the properties are not defined correctly
    Code:
            Private Class DefHolidays
                Property Name As String
                Property HolidayMonth As Integer = -1
                Property HolidayDayOfMonth As Integer = -1
                Property HolidayDate As DateTime
                Property Nth As Integer = -1 '-1 last NthDayOfWeek <<<<<<<<<<
                Property NthDayOfWeek As DayOfWeek
                Property EasterCalc As Boolean = False
                Property enabled As Boolean = True
            End Class

  5. #5
    PowerPoster
    Join Date
    May 2002
    Posts
    26,770

    Re: Calculate Holidays

    Those are using default property syntax... something that wasn't available until VS2010 I think it was... you'll need to expand those out into full properties complete with backing fields.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,038

    Re: Calculate Holidays

    Quote Originally Posted by BrailleSchool View Post
    What version of VB is this written in?
    When I paste the code into VB 2005, I am told the properties are not defined correctly
    Code:
            Private Class DefHolidays
                Property Name As String
                Property HolidayMonth As Integer = -1
                Property HolidayDayOfMonth As Integer = -1
                Property HolidayDate As DateTime
                Property Nth As Integer = -1 '-1 last NthDayOfWeek <<<<<<<<<<
                Property NthDayOfWeek As DayOfWeek
                Property EasterCalc As Boolean = False
                Property enabled As Boolean = True
            End Class
    2005 didn't have automatic getter / setters. If you want to use this in 2005 you will need to change each property. For example here is how name would look.
    Code:
        Private _name As String 'backing field
        Property Name As String
            Get
                Return Me._name
            End Get
            Set(value As String)
                Me._name = value
            End Set
        End Property
    Last edited by dbasnett; Jan 6th, 2015 at 02:29 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  7. #7
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,863

    Re: Calculate Holidays

    Quote Originally Posted by dbasnett View Post
    2005 didn't have automatic getter / setters. If you want to use this in 2005 you will need to change each property. For example here is how name would look.
    Code:
        Private _name As String 'backing field
        Property Name As String
            Get
                Return Me._name
            End Get
            Set(value As String)
                Me._name = value
            End Set
        End Property
    I am not familiar with Get/Setters much. So, they would look like so?
    Code:
        Private Class DefHolidays
            Private _Name As String
            Private _HolidayMonth As Integer = -1
            Private _HolidayDayOfMonth As Integer = -1
            Private _HolidayDate As DateTime
            Private _Nth As Integer = -1
            Private _NthDayOfWeek As DayOfWeek
            Private _EasterCalc As Boolean = False
            Private _Enabled As Boolean = True
    
            Property NthDayOfWeek() As DayOfWeek
                Get
                    Return Me._NthDayOfWeek
                End Get
                Set(ByVal value As DayOfWeek)
                    Me._NthDayOfWeek = value
                End Set
            End Property
    
            Property Enabled() As Boolean
                Get
                    Return Me._Enabled
                End Get
                Set(ByVal value As Boolean)
                    Me._Enabled = value
                End Set
            End Property
    
            Property EasterCalc() As Boolean
                Get
                    Return Me._EasterCalc
                End Get
                Set(ByVal value As Boolean)
                    Me._EasterCalc = value
                End Set
            End Property
    
            Property Name() As String
                Get
                    Return Me._name
                End Get
                Set(ByVal value As String)
                    Me._name = value
                End Set
            End Property
    
            Property HolidayMonth() As Integer
                Get
                    Return Me._HolidayMonth
                End Get
                Set(ByVal value As Integer)
                    Me._HolidayMonth = value
                End Set
            End Property
    
            Property HolidayDayOfMonth() As Integer
                Get
                    Return Me._HolidayDayOfMonth
                End Get
                Set(ByVal value As Integer)
                    Me._HolidayDayOfMonth = value
                End Set
            End Property
    
            Property HolidayDate() As DateTime
                Get
                    Return Me._HolidayDate
                End Get
                Set(ByVal value As DateTime)
                    Me._HolidayDate = value
                End Set
            End Property
    
            Property Nth() As Integer
                Get
                    Return Me._Nth
                End Get
                Set(ByVal value As Integer)
                    Me._Nth = value
                End Set
            End Property
        End Class
    ??

  8. #8

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,038

    Re: Calculate Holidays

    I didn't check them all but that looks correct.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.