I was going through some reports that were supposed to be displaying 2/1/2009 – 2/28/2009, but instead was displaying 2/1/2009 – 3/1/2009. I have narrowed the bug down to this code, any suggestions?
Function GetMonthLastDate(ByVal sDateTime As DateTime) Try Dim strArrMonth() As String = {'', '31', '29', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31'} Dim days As Integer If sDateTime.Month = 2 Then If sDateTime.Year Mod 400 = 0 Then Return sDateTime.AddDays(28) If sDateTime.Year Mod 100 = 0 Then Return sDateTime.AddDays(27) If sDateTime.Year Mod 4 = 0 Then Return sDateTime.AddDays(28) End If days = strArrMonth(sDateTime.Month) Return Format(sDateTime.AddDays(days - 1), 'MM/dd/yyyy') Catch ex As Exception Response.Write('<script>alert('' & ex.Message & '');</script>') End Try End Function
This is some seriously convoluted code for what it’s doing. You can accomplish the same thing like this:
That might be a little cryptic, but it should work and can be expanded for clarity.