Tuesday, 29 July 2014

c# example – Datetime Days in month

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 :
  1. <%@ Page Language=“C#” AutoEventWireup=“true”%>

  2. <!DOCTYPE html>
  3. <script runat=“server”>
  4.     protected void Button1_Click(object sender, System.EventArgs e)
  5.     {
  6.         //initialize a datetime variable with today
  7.         DateTime today = DateTime.Today;

  8.         //get number of days in current month
  9.         int numberOfDays = DateTime.DaysInMonth(today.Year,today.Month);

  10.         Label1.Text = “today : “ + today.ToLongDateString();

  11.         Label1.Text += “<br /><br />days in current month : “;
  12.         Label1.Text += numberOfDays;
  13.     }
  14. </script>

  15. <html xmlns=“http://www.w3.org/1999/xhtml”>
  16. <head id=“Head1″ runat=“server”>
  17.     <title>c# example - datetime days in month</title>
  18. </head>
  19. <body>
  20.     <form id=“form1″ runat=“server”>
  21.     <div>
  22.         <h2 style=“color:MidnightBlue; font-style:italic;”>
  23.             c# example - datetime days in month
  24.         </h2>
  25.         <hr width=“550″ align=“left” color=“Gainsboro” />
  26.         <asp:Label
  27.             ID=“Label1″
  28.             runat=“server”
  29.             Font-Size=“Large”
  30.             Font-Names=“Comic Sans MS”
  31.             >
  32.         </asp:Label>
  33.         <br /><br />
  34.         <asp:Button
  35.             ID=“Button1″
  36.             runat=“server”
  37.             Text=“get number of days in current month”
  38.             OnClick=“Button1_Click”
  39.             Height=“40″
  40.             Font-Bold=“true”
  41.             />
  42.     </div>
  43.     </form>
  44. </body>
  45. </html>

No comments:

Post a Comment