c# - How to get the last day of month using a given date

DateTime get last day of month
The following asp.net c# example code demonstrate us how can we get last day of a month programmaticallyat run time in an asp.net application. In this example code, we will see how can we get last day of a month froma specified DateTime object.

By default, .net framework's DateTime class has no method or property to get last day of a month. So, we need to applyfew techniques to get last day of a month from a given date. At first, we need to extract the month from a given date. Thenwe need to get the last day of extracted month.

We applied the following techniques to get the last day of a month; first, we add one month with the given date by usingDateTime.AddMonths() method. Then we create a new DateTime variable by using the month added DateTime object's year, monthand first day (day number 1) such as 2014/12/1(DateTime.Year/DateTime.Month/1).

So, we get a new DateTime object which represent first day of a month. Next, we subtract one day from this newlycreated DateTime object (first day of month). It returns the last day of previous month,when we subtract one day from first day of a month.

Finally, we get the last day of a month from a specified DateTime object. Because, at first we added one month with givenDateTime object.
datetime-get-last-day-of-month.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  

<!DOCTYPE html>      
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a datetime variable with today
        DateTime today = DateTime.Today;

        //create a datetime object to generate last day of month
        DateTime tempDate = today.AddMonths(1);
        DateTime tempDate2 = new DateTime(tempDate.Year,tempDate.Month,1);
        DateTime lastDayOfMonth = tempDate2.AddDays(-1);

        Label1.Text = "Today : " + today.ToLongDateString();
        Label1.Text += "<br /><br />last day of month : ";
        Label1.Text += lastDayOfMonth.ToLongDateString();
    }
</script>      

<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - datetime get last day of month</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - datetime get last day of month
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="Large"    
            Font-Names="Comic Sans MS"
            >      
        </asp:Label>      
        <br /><br />    
        <asp:Button       
            ID="Button1"       
            runat="server"       
            Text="get last day of month"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>