Monday, September 14, 2009

How to get the end of month date ?

There is a common requirement to extract the end of month date from a given date.



For eg. if I have a date of "15/09/2009" (dd/mm/yyyy) I would like to know what is the last date of this month which is "30/09/2009"



Below is a simple code to do this:


DateTime dtToday = DateTime.Today;
DateTime dtEndOfMonth;
if (dtToday.Month != 12)
dtEndOfMonth = new DateTime(dtToday.Year, dtToday.Month + 1, 1).AddDays(-1);
else
dtEndOfMonth = new DateTime(dtToday.Year + 1, 1, 1).AddDays(-1);


Hope this helps !

No comments: