Sometimes we need to know programmatically
that how many days exists in current month from a date time object. or
how many days exists in any specific month. we are know that in july
there are 31 days and in june there are 30 days. but how can we get it
by .net programming? there is a simple solution. here we usees a date
time function method name DaysInMonth. pass the two argument year and
month number. it will output the exact days number.
datetime-days-in-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;
- //get number of days in current month
- int numberOfDays = DateTime.DaysInMonth(today.Year,today.Month);
- Label1.Text = “today : “ + today.ToLongDateString();
- Label1.Text += “<br /><br />days in current month : “;
- Label1.Text += numberOfDays;
- }
- </script>
- <html xmlns=“http://www.w3.org/1999/xhtml”>
- <head id=“Head1″ runat=“server”>
- <title>c# example - datetime days in month</title>
- </head>
- <body>
- <form id=“form1″ runat=“server”>
- <div>
- <h2 style=“color:MidnightBlue; font-style:italic;”>
- c# example - datetime days in 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 number of days in current month”
- OnClick=“Button1_Click”
- Height=“40″
- Font-Bold=“true”
- />
- </div>
- </form>
- </body>
- </html>