c# 日历实现

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("

");

for (int i = 0; i < OneInWeek; i++)
{
sb.Append("");
}
for (int i = 1; i <= Days; i++)
{
sb.Append("");
if ((i + OneInWeek) % 7 == 0)
{
sb.Append("");
}
}
if (LastInWeek != 0)
{
for (int i = 1; i <= (7 - LastInWeek); i++)
{
sb.Append("");
}
}
sb.Append("
");
sb.Append("" + (PDays + i - OneInWeek + 1).ToString() + "");
sb.Append("
");
sb.Append(i == day ? "" + i.ToString() + "" : i.ToString());
sb.Append("
");
sb.Append("" + i.ToString() + "");
sb.Append("
");

}
catch
{ }
return sb.ToString();
}
}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/03/08/1680953.html,如需转载请自行联系原作者

你可能感兴趣的:(c# 日历实现)