欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#得到本月有几周和这几周的起止时间示例代码

程序员文章站 2024-02-25 21:57:57
示例代码:复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codebehind="default.as...

示例代码:

复制代码 代码如下:

<%@ 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>无标题页</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>
             /// 获得本月有几周
             /// </summary>
             /// <param name="a"></param>
             /// <returns></returns>
             public  void numweeks(datetime dt)
             {
                 //年
                 int year = dt.year;
                 //月
                 int month = dt.month;
                 //当前月第一天
                 datetime weekstart = new datetime(year, month, 1);
                 //该月的最后一天
                 datetime monend = weekstart.addmonths(1).adddays(-1);
                 int i = 1;
                 //当前月第一天是星期几
                 int dayofweek = convert.toint32(weekstart.dayofweek.tostring("d"));
                 //该月第一周结束日期
                 datetime weekend = dayofweek == 0 ? weekstart : weekstart.adddays(7 - dayofweek);

                 richtextbox2.text += "第" + i + "周起始日期: " + weekstart.toshortdatestring() + "   结束日期: " + weekend.toshortdatestring() + "\n";

                 //当日期小于或等于该月的最后一天
                 while (weekend.adddays(1) <= monend)
                 {
                     i++;
                     //该周的开始时间
                     weekstart = weekend.adddays(1);
                     //该周结束时间
                     weekend = weekend.adddays(7) > monend ? monend : weekend.adddays(7);

                     richtextbox2.text += "第" + i + "周起始日期: " + weekstart.toshortdatestring() + "   结束日期: " + weekend.toshortdatestring() + "\n";
                 }

                 richtextbox2.text += year + "年" + month + "月共有" + i + "周\n";
             }

         </script>
        

            <asp:textbox id="richtextbox2" runat="server" textmode="multiline" height="321px"
                width="845px" ></asp:textbox>

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