c gets sample code for the number of weeks this month and the start and end times of those weeks

  • 2020-06-03 08:06:12
  • OfStack

Sample code:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title> No title page </title>
    <style type="text/css" >
    .hidden{ display:none;}
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
   <script type="text/javascript">

   </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>

        <%
                NumWeeks(new DateTime(2009, 11, 15));

         %>

         <script runat="server" type="text/C#">

             /// <summary>
             ///  Get a few weeks in this month 
             /// </summary>
             /// <param name="a"></param>
             /// <returns></returns>
             public  void NumWeeks(DateTime dt)
             {
                 // years 
                 int year = dt.Year;
                 // month  
                 int month = dt.Month;
                 // The current month 1 day 
                 DateTime weekStart = new DateTime(year, month, 1);
                 // The end of the month 1 day 
                 DateTime monEnd = weekStart.AddMonths(1).AddDays(-1);
                 int i = 1;
                 // The current month 1 What day of the week is it 
                 int dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d"));
                 // The first month 1 End date of week 
                 DateTime weekEnd = dayOfWeek == 0 ? weekStart : weekStart.AddDays(7 - dayOfWeek); 

                 richTextBox2.Text += " The first " + i + " Week start Date:  " + weekStart.ToShortDateString() + "    End date:  " + weekEnd.ToShortDateString() + "\n";

                 // When the date is less than or equal to the end of the month 1 day 
                 while (weekEnd.AddDays(1) <= monEnd)
                 {
                     i++;
                     // The start time of the week 
                     weekStart = weekEnd.AddDays(1);
                     // End of the week 
                     weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7);

                     richTextBox2.Text += " The first " + i + " Week start Date:  " + weekStart.ToShortDateString() + "    End date:  " + weekEnd.ToShortDateString() + "\n";
                 }

                 richTextBox2.Text += year + " years " + month + " In a total of " + i + " weeks \n";
             }
         </script>
         
            <asp:TextBox ID="richTextBox2" runat="server" TextMode="MultiLine" Height="321px" 
                Width="845px" ></asp:TextBox>

        </div>
    </form>
</body>
</html>


Related articles: