ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

EndOfTheMonth

Returns datetime corresponding to last day of the month

Source

public static DateTime EndOfTheMonth(this DateTime date)
{
	var endOfTheMonth = new DateTime(date.Year, date.Month, 1)
		.AddMonths(1)
		.AddDays(-1);

	return endOfTheMonth;
}

Example

var lastDay = DateTime.Now.EndOfTheMonth();

Author: Victor Rodrigues

Submitted on: 13 mei 2009

Language: C#

Type: System.DateTime

Views: 3951