;

How to Get a Total Number of Days in a Month in C#


Tutorialsrack 02/04/2020 C# Programs

In this article, we will learn how to get a total number of days in the month in c#. There are two built-in methods to get the total number of days in a month. The first method is DateTime.DaysInMonth(year, month) and the second method is CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year,month).

Here are these examples to get the number of days in a month as follows:

Example 1: In this example, we will use DateTime.DaysInMonth(year, month) and this method takes two parameters: year and month. 

Example 1
using System;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get Total Number of Days in a Month in C# */
		static void Main(string[] args)
        {
			int days = DateTime.DaysInMonth(2020,02);
			Console.WriteLine("Total Number of Days in Month: {0} Days", days);

			//Hit ENTER to exit the program
			Console.ReadKey();
		}        
    }
}
Output

Total Number of Days in Month: 29 Days

Example 2: In this example, we will use CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year,month) and this method takes two parameters: year and month. For this method, you need to use  System.Globalization namespace.

Example 2
using System;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get Total Number of Days in a Month in C# */
		static void Main(string[] args)
        {
			int days = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(2020,02);
			Console.WriteLine("Total Number of Days in Month: {0} Days", days);

			//Hit ENTER to exit the program
			Console.ReadKey();
		}        
    }
}
Output

Total Number of Days in Month: 29 Days

I hope this article will help you to understand how to get a total number of days in the month in c#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags