w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Number of days in the specified month and year

C# Sharp DateTime: Exercise-19 with Solution

Write a C# Sharp program to get the number of days of the specified month and year.

Sample Solution:-

C# Sharp Code:

using System;
public class Example19
{
	static void Main()
	{
		const int July = 7;
		const int Feb = 2;

		int daysInJuly = System.DateTime.DaysInMonth(2016, July);
        Console.WriteLine(daysInJuly);

		// 2013 was not a leap year.
		int daysInFeb = System.DateTime.DaysInMonth(2013, Feb);
        Console.WriteLine(daysInFeb);

		// 2004 was a leap year.
		int daysInFebLeap = System.DateTime.DaysInMonth(2004, Feb);
		Console.WriteLine(daysInFebLeap);
	}
}

Sample Output:

31                                                                                                      
28                                                                                                      
29

C# Sharp Code Editor:

using System;
public class Example19
{
	static void Main()
	{
		const int July = 7;
		const int Feb = 2;

		int daysInJuly = System.DateTime.DaysInMonth(2016, July);
        Console.WriteLine(daysInJuly);

		// 2013 was not a leap year.
		int daysInFeb = System.DateTime.DaysInMonth(2013, Feb);
        Console.WriteLine(daysInFeb);

		// 2004 was a leap year.
		int daysInFebLeap = System.DateTime.DaysInMonth(2004, Feb);
		Console.WriteLine(daysInFebLeap);
	}
}

Improve this sample solution and post your code through Disqus