Thursday, November 16, 2006

VB.NET Get Last Day in Month Function

I recently had to ensure that a date in my code was the last day in the month. I know this is a simple thing to do, but I figured I could save myself five minutes by Googling for and getting a function to do this off the internet. Suprisingly, I was not able to find this code easily. So, here is the function I wrote, which hopefully will save a few minutes for another lazy programmer or two:


Private Function GetLastDayInMonth(ByVal dDate As Date) As Date
dDate = DateAdd(DateInterval.Month, 1, dDate)
dDate = Convert.ToDateTime(Month(dDate).ToString() & "/" & "1/" & Year(dDate).ToString())
dDate = DateAdd(DateInterval.Day, -1, dDate)
Return dDate
End Function

No comments: