首页 > 编程 > .NET > 正文

asp.net 获取某个时间段的星期并以表格形式排列出来

2024-07-10 12:42:30
字体:
来源:转载
供稿:网友
代码如下:
代码如下:
string start = Request.Form.Get("date2").ToString(); //获取开始日期,此控件为html控件
string end = Request.Form.Get("date3").ToString(); //获取结束日期,此控件为html控件
string[] start_date = start.Split('-'); //分离出开始日期的年、月、日
string[] end_date = end.Split('-'); //分离出结束日期的年、月、日
DateTime dt1 = new DateTime(int.Parse(start_date[0]), int.Parse(start_date[1]), int.Parse(start_date[2]));
DateTime dt2 = new DateTime(int.Parse(end_date[0]), int.Parse(end_date[1]), int.Parse(end_date[2]));
TimeSpan ts = dt2 - dt1;
//定义全局变量public string table_td = "";然后进行赋值
table_td = "<table><tr><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td><td>星期天</td></tr>";
//判断开始时间为星期几,如,如果是星期三,则需要前面空出2个单元格出来,以此类推
switch (dt1.DayOfWeek.ToString())
{
case "Tuesday":
table_td += "<tr><td></td>";
break;
case "Wednesday":
table_td += "<tr><td></td><td></td>";
break;
case "Thursday":
table_td += "<tr><td></td><td></td><td></td>";
break;
case "Friday":
table_td += "<tr><td></td><td></td><td></td><td></td>";
break;
case "Saturday":
table_td += "<tr><td></td><td></td><td></td><td></td><td></td>";
break;
case "Sunday":
table_td += "<tr><td></td><td></td><td></td><td></td><td></td><td></td>";
break;
}
//递归至结束时间
for (int i = 0; i < ts.Days; i++)
{
string sdate = dt1.AddDays(i).Day.ToString();
switch (dt1.AddDays(i).DayOfWeek.ToString())
{
case "Monday":
table_td += "<tr><td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Tuesday":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Wednesday":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Thursday":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Friday":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Saturday":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
break;
case "Sunday":
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表