How to get total days of month in c#

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#

Posted on by Jackrin Reacher | No comments


Post a Comment