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
Flowchart:

C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to compare the current date with a given date.
Next: Write a C# Sharp program to compare DateTime objects.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion