August 1st, 2010, 12:52 AM
Get date of Sunday within a month
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?
August 2nd, 2010, 02:02 AM
Re: Get date of Sunday within a month
Something like this :
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)
' Then return date of first sunday of month
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
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.