To calculate the number of days in a month in C# and VB.NET.

Sample C#

private static int DayCountPerMonth(DateTime date)
{
	if (date == null) {  throw new ArgumentNullException();}
	var calendar = new System.Globalization.GregorianCalendar();
	return calendar.GetDaysInMonth(date.Year, date.Month);
}

Sample VB.NET

Private Shared Function DayCountPerMonth(date As DateTime) As Integer
	If date Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim calendar = New System.Globalization.GregorianCalendar()
	Return calendar.GetDaysInMonth(date.Year, date.Month)
End Function

One thought on “How to calculate the number of days in a month in C# and VB.NET”

Leave a Reply