Get date of Sunday within a month
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Get date of Sunday within a month

  1. #1
    Join Date
    Jan 2009

    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?

  2. #2
    Join Date
    Jul 2001
    Sunny South Africa

    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)
            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

Posting Permissions

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

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center