源代码释放
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace 日历哦快乐
{
public partial class _Default : System.Web.UI.Page
{
StringBuilder Table = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = Calendar(DateTime.Now.Year, DateTime.Now.Month);
}
月份与星期#region 月份与星期
private string[] ds_monthnames = {
"一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月"
};
private string[] ds_daynames = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
};
#endregion
Table操作#region Table操作
/**//// <summary>
/// 返回Table头
/// </summary>
/// <returns></returns>
private string TableTop()
{
return "<table border='0' cellspacing='0' class='ds_box'>" +
"<tr class='ds_subhead'>";
}
/**//// <summary>
/// 返回Table中
/// </summary>
/// <returns></returns>
private string TableMiddle()
{
return "</tr><tr class='ds_cell'>";
}
/**//// <summary>
/// 返回Table尾
/// </summary>
/// <returns></returns>
private string TableEnd()
{
return "</tr></table>";
}
public string TableWeek(string t)
{
return "<td class='ds_subhead'><center>" + t + "</center></td>";
}
private string retTD()
{
return "<td class='ds_cell'> </td>";
}
#endregion
返回当月第一天的位置#region 返回当月第一天的位置
private int ReturnTableWeek(int y, int m)
{
int i = 7;
i = Convert.ToInt32(Convert.ToDateTime(y.ToString() + "-" + m.ToString() + "-" + "1" + " 00:00:00").DayOfWeek);
switch (i)
{
case 1:
return 2; break;//星期一返回为第二个位置
case 2:
return 3; break;
case 3:
return 4; break;
case 4:
return 5; break;
case 5:
return 6; break;
case 6:
return 7; break;
case 7:
return 1; break;
default:
return 1;
}
}
#endregion
主函数#region 主函数
public string Calendar(int year, int month)
{
int ds_c_month = month;
int ds_c_year = year;
Table.Append(TableTop());
for (int week = 0; week <= 6; week++)
{
Table.Append(TableWeek(ds_daynames[week]));
}
Table.Append(TableMiddle());
int ti = ReturnTableWeek(ds_c_year, ds_c_month);//得到当月第一天
for (int k = 1; k < ti; k++)
{
Table.Append(retTD());
}
int tab = 0;
//ti+ds_draw_calendar(ds_c_year,ds_c_month)
for (int i = ti; i < ti + ds_draw_calendar(ds_c_year, ds_c_month); i++)
{
Table.Append(ds_Content_calendar(ds_c_year, ds_c_month, i - ti + 1));
if (i % 7 == 0)
{
Table.Append(TableMiddle());
}
tab = i;
}
while (tab % 7 != 0)
{
tab += 1;
Table.Append(retTD());
}
Table.Append(TableEnd());
return Table.ToString();
}
#endregion
返回本月的天数#region 返回本月的天数
/**//// <summary>
/// 返回本月的天数
/// </summary>
/// <param name="m">月</param>
/// <param name="y">年</param>
private int ds_draw_calendar(int y, int m)
{
int days = 0;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
days = 31;
return days;
}
else
{
if (m == 4 || m == 6 || m == 9 || m == 11)
{
days = 30;
return days;
}
else
{
//四年一闰,百年不闰,四百年再闰
if (m == 2)
{
days = (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? 29 : 28;
return days;
}
else
{
return days;
}
}
}
}
#endregion
返回具体一天 数据库操作#region 返回具体一天 数据库操作
/**//// <summary>
/// 返回具体一天
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <param name="days">日</param>
/// <returns></returns>
public virtual string ds_Content_calendar(int year, int month, int days)
{
if (days == DateTime.Now.Day)
{
return "<td style='color:green;'>" + days.ToString() + "</td>";
}
else
{
return "<td>" + days.ToString() + "</td>";
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace 日历哦快乐
{
public partial class _Default : System.Web.UI.Page
{
StringBuilder Table = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = Calendar(DateTime.Now.Year, DateTime.Now.Month);
}
月份与星期#region 月份与星期
private string[] ds_monthnames = {
"一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月"
};
private string[] ds_daynames = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
};
#endregion
Table操作#region Table操作
/**//// <summary>
/// 返回Table头
/// </summary>
/// <returns></returns>
private string TableTop()
{
return "<table border='0' cellspacing='0' class='ds_box'>" +
"<tr class='ds_subhead'>";
}
/**//// <summary>
/// 返回Table中
/// </summary>
/// <returns></returns>
private string TableMiddle()
{
return "</tr><tr class='ds_cell'>";
}
/**//// <summary>
/// 返回Table尾
/// </summary>
/// <returns></returns>
private string TableEnd()
{
return "</tr></table>";
}
public string TableWeek(string t)
{
return "<td class='ds_subhead'><center>" + t + "</center></td>";
}
private string retTD()
{
return "<td class='ds_cell'> </td>";
}
#endregion
返回当月第一天的位置#region 返回当月第一天的位置
private int ReturnTableWeek(int y, int m)
{
int i = 7;
i = Convert.ToInt32(Convert.ToDateTime(y.ToString() + "-" + m.ToString() + "-" + "1" + " 00:00:00").DayOfWeek);
switch (i)
{
case 1:
return 2; break;//星期一返回为第二个位置
case 2:
return 3; break;
case 3:
return 4; break;
case 4:
return 5; break;
case 5:
return 6; break;
case 6:
return 7; break;
case 7:
return 1; break;
default:
return 1;
}
}
#endregion
主函数#region 主函数
public string Calendar(int year, int month)
{
int ds_c_month = month;
int ds_c_year = year;
Table.Append(TableTop());
for (int week = 0; week <= 6; week++)
{
Table.Append(TableWeek(ds_daynames[week]));
}
Table.Append(TableMiddle());
int ti = ReturnTableWeek(ds_c_year, ds_c_month);//得到当月第一天
for (int k = 1; k < ti; k++)
{
Table.Append(retTD());
}
int tab = 0;
//ti+ds_draw_calendar(ds_c_year,ds_c_month)
for (int i = ti; i < ti + ds_draw_calendar(ds_c_year, ds_c_month); i++)
{
Table.Append(ds_Content_calendar(ds_c_year, ds_c_month, i - ti + 1));
if (i % 7 == 0)
{
Table.Append(TableMiddle());
}
tab = i;
}
while (tab % 7 != 0)
{
tab += 1;
Table.Append(retTD());
}
Table.Append(TableEnd());
return Table.ToString();
}
#endregion
返回本月的天数#region 返回本月的天数
/**//// <summary>
/// 返回本月的天数
/// </summary>
/// <param name="m">月</param>
/// <param name="y">年</param>
private int ds_draw_calendar(int y, int m)
{
int days = 0;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
days = 31;
return days;
}
else
{
if (m == 4 || m == 6 || m == 9 || m == 11)
{
days = 30;
return days;
}
else
{
//四年一闰,百年不闰,四百年再闰
if (m == 2)
{
days = (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? 29 : 28;
return days;
}
else
{
return days;
}
}
}
}
#endregion
返回具体一天 数据库操作#region 返回具体一天 数据库操作
/**//// <summary>
/// 返回具体一天
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <param name="days">日</param>
/// <returns></returns>
public virtual string ds_Content_calendar(int year, int month, int days)
{
if (days == DateTime.Now.Day)
{
return "<td style='color:green;'>" + days.ToString() + "</td>";
}
else
{
return "<td>" + days.ToString() + "</td>";
}
}
#endregion
}
}