Hi,
Can anyone advice how can I get the date of first Sunday in a month?
For example in January, the first date of Sunday is 7. What code is needed to retrieve first Sunday date in a month?
Printable View
Hi,
Can anyone advice how can I get the date of first Sunday in a month?
For example in January, the first date of Sunday is 7. What code is needed to retrieve first Sunday date in a month?
Something like this :
Code:Public Class Form1
Public Function GetFirstSunday(ByVal intMonth As Integer, ByVal intYear As Integer) As DateTime
' Create a start date for the 1st day of the month
Dim dtStartDate As DateTime = New DateTime(intYear, intMonth, 1)
' Keep looping, adding dayss to the start date until sunday is reached
While dtStartDate.DayOfWeek <> DayOfWeek.Sunday
dtStartDate = dtStartDate.AddDays(1)
End While
' Then return date of first sunday of month
Return dtStartDate
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(GetFirstSunday(8, 2010)) 'Returns First Sunday In August, 2010
End Sub
End Class