Int32 - DaysInMonth
Returns the number of days in the specified month and year.
public static void Main() { int[] years = { 2016, 2019 }; DateTimeFormatInfo dtfi = DateTimeFormatInfo.CurrentInfo; Console.WriteLine("Days in the Month for the {0} culture using the {1} calendar\n", CultureInfo.CurrentCulture.Name, dtfi.Calendar.GetType().Name.Replace("Calendar", "")); Console.WriteLine("{0,-10}{1,-15}{2, -4}\n", "Year", "Month", "Days"); foreach (var year in years) { for (int ctr = 0; ctr <= dtfi.MonthNames.Length - 1; ctr++) { if (String.IsNullOrEmpty(dtfi.MonthNames[ctr])) continue; // C# Extension Method: Int32 - DaysInMonth Console.WriteLine("{0,-10}{1,-15}{2, -4}", year, dtfi.MonthNames[ctr], year.DaysInMonth(ctr + 1)); } Console.WriteLine(); } }
View Source
using System; public static partial class Extensions { /// <summary> /// Returns the number of days in the specified month and year. /// </summary> /// <param name="year">The year.</param> /// <param name="month">The month (a number ranging from 1 to 12).</param> /// <returns> /// The number of days in for the specified .For example, if equals 2 for February, the return value is 28 or /// 29 depending upon whether is a leap year. /// </returns> public static Int32 DaysInMonth(this Int32 year, Int32 month) { return DateTime.DaysInMonth(year, month); } }