Wednesday, 15 August 2012

VB.NET - Calculate number of Sundays this year

        Dim dFirstDayOfTheYear As Date = CType("1/1/" & Date.Now.Year, Date)
        Dim dLastDayOfTheYear As Date = CType("12/31/" & Date.Now.Year, Date)
        Dim dFirstSundayOfTheYear As Date
        If dFirstDayOfTheYear.DayOfWeek > DayOfWeek.Sunday Then
            dFirstSundayOfTheYear = dFirstDayOfTheYear.AddDays(7 - dFirstDayOfTheYear.DayOfWeek)
        Else
            dFirstSundayOfTheYear = dFirstDayOfTheYear
        End If

        Dim intNumberOfSundaysThisYear As Integer = DateDiff(DateInterval.Day, dFirstSundayOfTheYear, dLastDayOfTheYear.AddDays(1)) / 7
        Response.Write("Number of Sundays this year: " & intNumberOfSundaysThisYear.ToString)

More Info:

No comments:

Post a Comment