[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 860

# VB.NET calender and bank holidays

I have some code that works out bank holidays (for the uk).

they all show on my calender but 3...

these are:  EarlyMay(TheYear)
Spring(TheYear)
Summer(TheYear)

im not sure why these wont show as blank on my calender like the rest do.
``````Imports Microsoft.VisualBasic

Public Class bankholidays2
Shared Function ForYear(ByVal TheYear As Integer) As Array
Dim BankHolidays(7) As Date
BankHolidays(0) = NewYearsDay(TheYear)
Dim Easter() As Date = EasterFridayMonday(TheYear)
BankHolidays(1) = Easter(0)
BankHolidays(2) = Easter(1)
BankHolidays(3) = EarlyMay(TheYear)
BankHolidays(4) = Spring(TheYear)
BankHolidays(5) = Summer(TheYear)
BankHolidays(6) = ChristmasDay(TheYear)
BankHolidays(7) = BoxingDay(TheYear)
Return BankHolidays
End Function
Shared Function NewYearsDay(ByVal TheYear As Integer) As Date
Dim StartDate As Date = "01-Jan-" & TheYear
Do
If IsWeekend(StartDate) Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Shared Function EasterSunday(ByVal TheYear As Integer) As Date
Dim a, b, c, d, e, f, g, h, i, k, L, m, month, day As Integer
a = TheYear Mod 19 : b = TheYear \ 100 : c = TheYear Mod 100 : d = b \ 4
e = b Mod 4 : f = (b + 8) \ 25 : g = (b - f + 1) \ 3
h = (19 * a + b - d - g + 15) Mod 30 : i = c \ 4 : k = c Mod 4
L = (32 + 2 * e + 2 * i - h - k) Mod 7 : m = (a + 11 * h + 22 * L) \ 451
month = (h + L - 7 * m + 114) \ 31 : day = ((h + L - 7 * m + 114) Mod 31) + 1
Dim Easter As Date = DateSerial(TheYear, month, day)
Return Easter
End Function
Shared Function EasterFridayMonday(ByVal TheYear As Integer) As Date()
Dim a, b, c, d, e, f, g, h, i, k, L, m, month, day As Integer
a = TheYear Mod 19 : b = TheYear \ 100 : c = TheYear Mod 100 : d = b \ 4
e = b Mod 4 : f = (b + 8) \ 25 : g = (b - f + 1) \ 3
h = (19 * a + b - d - g + 15) Mod 30 : i = c \ 4 : k = c Mod 4
L = (32 + 2 * e + 2 * i - h - k) Mod 7 : m = (a + 11 * h + 22 * L) \ 451
month = (h + L - 7 * m + 114) \ 31 : day = ((h + L - 7 * m + 114) Mod 31) + 1
Dim Easter As Date = DateSerial(TheYear, month, day)
Dim EasterMonday As Date = Easter.AddDays(1)
Dim GoodFriday As Date = Easter.AddDays(-2)
Dim EasterDates(1) As Date
EasterDates(0) = EasterMonday
EasterDates(1) = GoodFriday
Return EasterDates
End Function
Shared Function EarlyMay(ByVal theYear As Integer) As Date
'first Monday in May
Dim StartDate As Date = "01-May-" & theYear
Do
If WeekdayName(Weekday(StartDate)) <> "Monday" Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Shared Function Spring(ByVal theYear As Integer) As Date
'last Monday in May
Dim StartDate As Date = "31-May-" & theYear
Do
If WeekdayName(Weekday(StartDate)) <> "Monday" Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Shared Function Summer(ByVal theYear As Integer) As Date
'last Monday in August
Dim StartDate As Date = "31-Aug-" & theYear
Do
If WeekdayName(Weekday(StartDate)) <> "Monday" Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Shared Function ChristmasDay(ByVal theYear As Integer) As Date
Dim StartDate As Date = "25-Dec-" & theYear
Do
If IsWeekend(StartDate) Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Shared Function BoxingDay(ByVal theYear As Integer) As Date
Dim StartDate As Date = ChristmasDay(theYear).AddDays(1)
Do
If IsWeekend(StartDate) Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
Private Shared Function IsWeekend(ByVal DateValue As Object) As Boolean
Dim dDateValue As Date
dDateValue = CDate(DateValue)
IsWeekend = (Weekday(dDateValue) Mod 6 = 1)
End Function

End Class
``````

here is how i call my code

`````` Dim Easter() As Date = bankholidays2.EasterFridayMonday(e.Day.Date.Year)
If e.Day.Date = bankholidays2.NewYearsDay(e.Day.Date.Year) Or e.Day.Date = bankholidays2.EarlyMay(e.Day.Date.Year) Or e.Day.Date = Easter(0) Or e.Day.Date = Easter(1) Or e.Day.Date = bankholidays2.Spring(e.Day.Date.Year) Or e.Day.Date = bankholidays2.Summer(e.Day.Date.Year) Or e.Day.Date = bankholidays2.ChristmasDay(e.Day.Date.Year) Or e.Day.Date = bankholidays2.BoxingDay(e.Day.Date.Year) Then
pm.ImageUrl = "Images/smlhol.jpg"
am.ImageUrl = "Images/smlhol.jpg"
full.ImageUrl = "Images/fullhol.jpg"
am.Enabled = False
pm.Enabled = False
full.Enabled = False
End If
``````

I have also added an image here it shows april 2012 with the blank fields and highlighted where the code so not show the buttons on the 7th may (first monday in may)

Image1.jpg
0
runnerjp2005
• 2
1 Solution

Commented:
Did you step through the EarlyMay function to see if its returning a valid date?
0

Author Commented:
aha its not returning a valid date

`````` Shared Function EarlyMay(ByVal theYear As Integer) As Date
'first Monday in May
Dim StartDate As Date = "01-May-" & theYear
Do
If WeekdayName(Weekday(StartDate)) <> "Monday" Then
Else
Exit Do
End If
Loop
Return StartDate
End Function
``````

Return StartDate = 5/6/2012 when it should be 7th may 2012

0

Author Commented:
Dim Dte As Date = New Date(theYear, "05", 1)

Do While Dte.DayOfWeek <> DayOfWeek.Monday