using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class tt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(cld(DateTime.Now));
}
}
private int dw(DayOfWeek dw)
{
int result = 0;
switch (dw)
{
case DayOfWeek.Monday:
result = 1;
break;
case DayOfWeek.Tuesday:
result = 2;
break;
case DayOfWeek.Wednesday:
result = 3;
break;
case DayOfWeek.Thursday:
result = 4;
break;
case DayOfWeek.Friday:
result = 5;
break;
case DayOfWeek.Saturday:
result = 6;
break;
default:
result = 0;
break;
}
return result;
}
private string cld(DateTime dt)
{
StringBuilder sb = new StringBuilder();
try
{
int day = dt.Day;//当天
DateTime cdt = dt.AddDays(1 - dt.Day);//当月第一天
int OneInWeek = dw(cdt.DayOfWeek);//当月第一天星期
int Days = DateTime.DaysInMonth(dt.Year, dt.Month);//当月天数
DateTime ndt = dt.AddDays(1 - dt.Day).AddMonths(1);//下个月
int LastInWeek = dw(ndt.DayOfWeek);//下个月第一天星期
DateTime pdt = dt.AddMonths(-1);//上个月
int PDays = DateTime.DaysInMonth(pdt.Year, pdt.Month);//上个月天数
sb.Append("<table border='1'><tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr><tr>");
for (int i = 0; i < OneInWeek; i++)
{
sb.Append("<td>");
sb.Append("<font color='gray'>" + (PDays + i - OneInWeek + 1).ToString() + "</font>");
sb.Append("</td>");
}
for (int i = 1; i <= Days; i++)
{
sb.Append("<td>");
sb.Append(i == day ? "<font color='red'>" + i.ToString() + "</font>" : i.ToString());
sb.Append("</td>");
if ((i + OneInWeek) % 7 == 0)
{
sb.Append("</tr><tr>");
}
}
if (LastInWeek != 0)
{
for (int i = 1; i <= (7 - LastInWeek); i++)
{
sb.Append("<td>");
sb.Append("<font color='gray'>" + i.ToString() + "</font>");
sb.Append("</td>");
}
}
sb.Append("<tr></table>");
}
catch
{ }
return sb.ToString();
}
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/03/08/1680953.html,如需转载请自行联系原作者