How to get total days of month in asp.net 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">
    <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#

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

0 comments: