asp.net 获取某个时间段的星期并以表格形式排列出来
程序员文章站
2024-03-07 22:33:51
代码如下:复制代码 代码如下: string start = request.form.get("date2").tostring(); //获取开始日期,此控件为html...
代码如下:
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());
看完后,大致上的想法是一样的,只是.......初学者没办法。继续努力。
复制代码 代码如下:
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());
看完后,大致上的想法是一样的,只是.......初学者没办法。继续努力。