asp.net 擷取某個時間段的星期並以表格形式排列出來

來源:互聯網
上載者:User

代碼如下: 複製代碼 代碼如下: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":
table_td += "<td><a onmouseover=selStation(1); onmouseout=chide();>" + sdate + "</a></td></tr>";
break;
}
}
//結束
table_td += "</tr></table>";

列印table_td即可。
以上是我自己寫的,但覺得不怎麼樣,下面我提供下別人在我做完後,做給我的,代碼如下: 複製代碼 代碼如下:int weekIndex(string item)
{
string[] week = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int index = -1;
for (int i = 0; i < week.Length; i++)
{
if (week[i].Trim().ToUpper() == item.ToUpper().Trim())
{
index = i;
break;
}
}
return index;
}

複製代碼 代碼如下: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());

看完後,大致上的想法是一樣的,只是.......初學者沒辦法。繼續努力。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.