Monday, 30 July 2012

VB.NET - How to get the date of the first Sunday of the year

        Example how to get the date of the first Sunday of the year
        First we have to get first date of the year (january first)

        Dim dFirstDateOfTheYear As Date = CType("1/1/" & Date.Now.Year, Date)

        ' Remember that DayOfWeek.Sunday = 0
        Dim dFirstSundayOfTheYear As Date
        If dFirstDateOfTheYear.DayOfWeek > DayOfWeek.Sunday Then
            dFirstSundayOfTheYear = dFirstDateOfTheYear.AddDays(7 - dFirstDateOfTheYear.DayOfWeek)
        Else
            dFirstSundayOfTheYear = dFirstDateOfTheYear
        End If

        Response.Write("The Date Of FIrst Sunday of this Year (2012) " & dFirstSundayOfTheYear.ToShortDateString)
        ' it will print January 1

More info:

No comments:

Post a Comment