asp.net get the week of a certain time period and arrange it in tabular form _ Practical Tips

Source: Internet
Author: User
The code is as follows:
Copy Code code as follows:

String start = Request.Form.Get ("Date2"). ToString (); Get start date, this control is HTML control
String end = Request.Form.Get ("Date3"). ToString (); Gets the end date, which is the HTML control
string[] start_date = start. Split ('-'); Isolate the year, month, and day of the start date
string[] end_date = end. Split ('-'); Isolate the year, month, and day of the end date
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;
Defines a global variable public string table_td = "";
TABLE_TD = "<table><tr><td> Monday </td><td> Tuesday </td><td> Wednesday </td>< td> Thursday </td><td> Friday </td><td> Saturday </td><td> Sunday </td></tr> ";
The start time is the day of the week, for example, if it is Wednesday, you need to empty out the front 2 cells, and so on
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
}
Recursion to end time
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
}
}
End
TABLE_TD = "</tr></table>";

You can print table_td.
The above is my own writing, but feel bad, below I provide the next person after I finish, do to me, the code is as follows:
Copy Code code as follows:

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;
}

Copy Code code as follows:

StringBuilder sb = new StringBuilder ();
Sb. Remove (0, sb.) Length);
Sb. Append ("<table><tr><td> Monday </td><td> Tuesday </td><td> Wednesday </td><td > Thursday </td><td> Friday </td><td> Saturday </td><td> Sunday </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 ());

After reading, the general idea is the same, just ... Beginners have no choice. Keep trying.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.