asp.net 获取某个时间段的星期并以表格形式排列出来
今天遇到了个关于时间的操作,而作为初学者的我,并不是很了解时间函数的操作,所以就园里问了下别人,经别人的帮助我自己实现了如何在某个时间段获取星期。代码如下:
代码
1 string start = Request.Form.Get("date2").ToString(); //获取开始日期,此控件为html控件
2 string end = Request.Form.Get("date3").ToString(); //获取结束日期,此控件为html控件
3 string[] start_date = start.Split('-'); //分离出开始日期的年、月、日
4 string[] end_date = end.Split('-'); //分离出结束日期的年、月、日
5 DateTime dt1 = new DateTime(int.Parse(start_date[0]), int.Parse(start_date[1]), int.Parse(start_date[2]));
6 DateTime dt2 = new DateTime(int.Parse(end_date[0]), int.Parse(end_date[1]), int.Parse(end_date[2]));
7 TimeSpan ts = dt2 - dt1;
8 //定义全局变量public string table_td = "";然后进行赋值
9 table_td = "<table><tr><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td><td>星期天</td></tr>";
10 //判断开始时间为星期几,如,如果是星期三,则需要前面空出2个单元格出来,以此类推
11 switch (dt1.DayOfWeek.ToString())
12 {
13 case "Tuesday":
14 table_td += "<tr><td></td>";
15 break;
16 case "Wednesday":
17 table_td += "<tr><td></td><td></td>";
18 break;
19 case "Thursday":
20 table_td += "<tr><td></td><td></td><td></td>";
21 break;
22 case "Friday":
23 table_td += "<tr><td></td><td></td><td></td><td></td>";
24 break;
25 case "Saturday":
26 table_td += "<tr><td></td><td></td><td></td><td></td><td></td>";
27 break;
28 case "Sunday":
29 table_td += "<tr><td></td><td></td><td></td><td></td><td></td><td></td>";
30 break;
31 }
32 //递归至结束时间
33 for (int i = 0; i < ts.Days; i++)
34 {
35 string sdate = dt1.AddDays(i).Day.ToString();
36 switch (dt1.AddDays(i).DayOfWeek.ToString())
37 {
38 case "Monday":
39 table_td += "<tr><td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
40 break;
41 case "Tuesday":
42 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
43 break;
44 case "Wednesday":
45 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
46 break;
47 case "Thursday":
48 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
49 break;
50 case "Friday":
51 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
52 break;
53 case "Saturday":
54 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td>";
55 break;
56 case "Sunday":
57 table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td></tr>";
58 break;
59 }
60 }
61 //结束
62 table_td += "</tr></table>";
打印table_td即可。
以上是我自己写的,但觉得不怎么样,下面我提供下别人在我做完后,做给我的,代码如下:
代码
1 int weekIndex(string item)
2 {
3 string[] week = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
4 int index = -1;
5 for (int i = 0; i < week.Length; i++)
6 {
7 if (week[i].Trim().ToUpper() == item.ToUpper().Trim())
8 {
9 index = i;
10 break;
11 }
12 }
13 return index;
14 }
代码
StringBuilder sb = new StringBuilder();
sb.Remove(0, sb.Length);
sb.Append("<table><tr><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td><td>星期天</td></tr>");
DateTime dt1 = new DateTime(2010, 3, 16);
DateTime dt2 = new DateTime(2010, 4, 13);
TimeSpan ts = dt2 - dt1;
int index; DateTime dtTemp;
for (int i = 0; i < ts.Days; i++) {
dtTemp=dt1.AddDays(i);
index = weekIndex(dtTemp.DayOfWeek.ToString());
if (index == 0 && i > 0)
{
sb.AppendFormat("<tr><td>{0}</td>", dtTemp.ToString("MM-dd"));
}
else if (index > 0 && i == 0)
{
sb.Append("<tr>");
for (int j = 0; j <= index - 1; j++)
{
sb.Append("<td> </td>");
}
sb.AppendFormat("<td>{0}</td>", dtTemp.ToString("MM-dd"));
}
else if (index == 6 && i>0)
{
sb.AppendFormat("<td>{0}</td></tr>", dtTemp.ToString("MM-dd"));
}
else {
sb.AppendFormat("<td>{0}</td>", dtTemp.ToString("MM-dd"));
}
}
sb.Append("</table>");
Response.Write(sb.ToString());
看完后,大致上的想法是一样的,只是.......初学者没办法。继续努力。