How to get total days of month in asp.net 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">
    <div>  
        <asp:Button ID="Button1"
         runat="server"
         BackColor="#99CCFF"
         onclick="Button1_Click"
         Text="Click" />  
    </div>
       <asp:Label ID="Label1"
       runat="server"
       BackColor="#FFFF66"
       Text="Label"></asp:Label>
    </form>

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 asp.net c#

Posted on by Jackrin Reacher | No comments

0 comments:

Post a Comment