How to get total days of month in c#

May 23, 2014 0 Comments

If you want to find total days in current month then you should use DayInMonth( ) method of DateTime structure. In this method you should pass the year and month, which you want to access total days of the month. Lets take an simple example to find total days of a month

Source Code

<form id="form1" runat="server">
        <asp:Button ID="Button1"
         Text="Click" />  
       <asp:Label ID="Label1"

Code Behind

protected void Button1_Click(object sender, EventArgs e)
            DateTime today = DateTime.Today;          
            int TotalOfDays = DateTime.DaysInMonth(today.Year, today.Month);
            Label1.Text = "today : " + today.ToLongDateString();
            Label1.Text += "<br /><br />Total days in month=";
            Label1.Text += TotalOfDays;

Code Generate the following output
How to get total days of month in c#

Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google