. . Hong Kong Business Software Company
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

6 Dec 2008
Last Sunday of Month
Recently, I was requested by a client to show two clocks on a website. One clock shows Hongkong/China time and the other shows Europe time. If Europe does not have Summer time, this is quite easy because Hongkong/China time is GMT+8 and Europe time is GMT+1.

Summer time in Europe (Central European Summer Time) is used between last Sunday in March and last Sunday in October. So, I need to find the last Sunday in March and last Sunday in October for converting Hongkong/China time to Europe time. I, therefor, need to write a LastSundayOfMonth function. The code in VB.Net is as follows:
    Public Function LastSundayOfMonth(ByVal d As Date) As Date
        Dim lastDateOfMonth As Date = _
            CDate(Date.DaysInMonth(d.Year, d.Month).ToString & _
            " " & d.ToString("MMM yyyy"))
            
        For i As Integer = 0 To 6
            If lastDateOfMonth.AddDays(-1 * i).DayOfWeek = DayOfWeek.Sunday Then
                Return lastDateOfMonth.AddDays(-1 * i)
            End If
        Next

    End Function