Asp.net 万年历
作者:互联网
Calendar控件扩展的万年历,包括阴阳历还可以标记节日、节气。
1、Calendar.aspx.cs
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.Globalization;
using System.Text;
public partial class Calendar : System.Web.UI.Page
{
string[,] holidays = new String[13, 32];
string[,] Chinaholidays = new String[13, 32];
string[,] GoodDays = new String[13,32];
private static ChineseLunisolarCalendar ChinaCalendar = new ChineseLunisolarCalendar();
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
string aHoliday;
string aChinaHoliday;
DateTime theDate = e.Day.Date;
int theChinaMonth = ChinaCalendar.GetMonth(theDate)-1;
int ThisYear = Convert.ToInt32(DateTime.Now.Year);
if (ThisYear % 400 == 0 || (ThisYear % 4 == 0 && ThisYear % 100 != 0))
theChinaMonth = theChinaMonth + 1;
int theChinaDay = ChinaCalendar.GetDayOfMonth(theDate);
string ChinaMD = ChineseCalendarHelper.GetMonth(theDate) + "月" + ChineseCalendarHelper.GetDay(theDate);
string aGoodDay;
//Response.Write(theDate.ToString()+"<BR>");
//Response.Write(theChinaMonth.ToString()+"<br>");
//Response.Write(theChinaDay.ToString());
aChinaHoliday = Chinaholidays[theChinaMonth,theChinaDay];
aHoliday = holidays[theDate.Month, theDate.Day];
aGoodDay = GoodDays[theDate.Month, theDate.Day];
if (aHoliday != null)
{
Label aLabel = new Label();
aLabel.Font.Size = 10;
aLabel.ForeColor = System.Drawing.Color.Red;
aLabel.Text = "<br>["+aHoliday+"]";
e.Cell.Controls.Add(aLabel);
}
if (aChinaHoliday != null)
{
Label aLabel = new Label();
aLabel.Font.Size = 10;
aLabel.ForeColor = System.Drawing.Color.Red;
aLabel.Text = "<br>[" + aChinaHoliday + "]";
e.Cell.Controls.Add(aLabel);
}
if (aChinaHoliday == null && aHoliday == null)
{
Label aLabel = new Label();
aLabel.Font.Size = 9;
//aLabel.ForeColor = System.Drawing.Color.Black;
aLabel.Font.Bold = false;
aLabel.Text = "<br>" + ChinaMD + "";
e.Cell.Controls.Add(aLabel);
}
//
string Sql = "Select GDay From GoodDays";
string[] GoodDaysCo = DataOK.IDArray(Sql);
foreach (string i in GoodDaysCo)
{
//Response.Write(Convert.ToDateTime(i).ToShortDateString().ToString() + "<br>");
if (theDate.ToShortDateString() == Convert.ToDateTime(i).ToShortDateString())
{
Label aLabel = new Label();
aLabel.Font.Size = 8;
aLabel.ForeColor = System.Drawing.Color.Red;
aLabel.Text = "<br>[宜嫁娶]";
e.Cell.Controls.Add(aLabel);
e.Cell.BackColor = System.Drawing.Color.MistyRose;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
//国际节
holidays[1, 1] = "元旦";
holidays[2, 14] = "情人节";
holidays[3, 8] = "妇女节";
holidays[3, 15] = "消费者权益日";
holidays[4, 1] = "愚人节";
holidays[5, 1] = "劳动节";
holidays[5, 4] = "青年节";
holidays[6, 1] = "儿童节";
holidays[7, 1] = "党的生日";
holidays[8, 1] = "建军节";
holidays[9, 10] = "教师节";
holidays[10, 1] = "国庆节";
holidays[12, 24] = "平安夜";
holidays[12, 25] = "圣诞节";
//中国节
Chinaholidays[1, 1] = "春节";
Chinaholidays[1, 15] = "元宵节";
Chinaholidays[4, 5] = "清明节";
Chinaholidays[9, 9] = "重阳";
Chinaholidays[12, 30] = "除夕";
Chinaholidays[5, 5] = "端午节";
Chinaholidays[7, 7] = "七夕";
Chinaholidays[7, 15] = "鬼节";
Chinaholidays[8, 15] = "中秋节";
Chinaholidays[12, 8] = "腊八节";
Chinaholidays[7, 15] = "鬼节";
this.Calendar1.Caption = " 农历今天是:" + ChineseCalendarHelper.GetStemBranch(DateTime.Now)+ "年" + ChineseCalendarHelper.GetMonth(DateTime.Now) + "月" + ChineseCalendarHelper.GetDay(DateTime.Now) + " 生肖:" + ChineseCalendarHelper.GetSX(DateTime.Now);
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
string Day = this.Calendar1.SelectedDate.ToShortDateString().ToString();
string GContent = DataOK.Select("Select * From GoodDays Where GDay='" + Day + "'", "GContent");
//Response.Write(GContent);
if (GContent == null || GContent == "")
this.Label1.Text = "无当日资料";
else
this.Label1.Text = GContent;
}
}
2、农历类文件1:ChineseCalendarFormatter.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;
/// <summary>
/// 自定义格式化器
/// </summary>
public class ChineseCalendarFormatter : IFormatProvider, ICustomFormatter
{
//实现IFormatProvider
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return Thread.CurrentThread.CurrentCulture.GetFormat(formatType);
}
//实现ICustomFormatter
public string Format(string format, object arg, IFormatProvider formatProvider)
{
string s;
IFormattable formattable = arg as IFormattable;
if (formattable == null)
s = arg.ToString();
else
s = formattable.ToString(format, formatProvider);
if (arg.GetType() == typeof(DateTime))
{
DateTime time = (DateTime)arg;
switch (format)
{
case "D": //长日期格式
s = String.Format("{0}年{1}月{2}",
ChineseCalendarHelper.GetYear(time),
ChineseCalendarHelper.GetMonth(time),
ChineseCalendarHelper.GetDay(time));
break;
case "d": //短日期格式
s = String.Format("{0}年{1}月{2}", ChineseCalendarHelper.GetStemBranch(time),
ChineseCalendarHelper.GetMonth(time),
ChineseCalendarHelper.GetDay(time));
break;
case "M": //月日格式
s = String.Format("{0}月{1}", ChineseCalendarHelper.GetMonth(time),
ChineseCalendarHelper.GetDay(time));
break;
case "Y": //年月格式
s = String.Format("{0}年{1}月", ChineseCalendarHelper.GetYear(time),
ChineseCalendarHelper.GetMonth(time));
break;
default:
s = String.Format("{0}年{1}月{2}", ChineseCalendarHelper.GetYear(time),
ChineseCalendarHelper.GetMonth(time),
ChineseCalendarHelper.GetDay(time));
break;
}
}
return s;
}
}
3、农历类文件2:ChineseCalendarHelper.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
/// <summary>
/// ChineseCalendarHelper 的摘要说明
/// 封装了农历的一些常用字符和对日历处理的最基本功能
/// </summary>
public class ChineseCalendarHelper
{
//计算中国年
public static string GetYear(DateTime time)
{
StringBuilder sb = new StringBuilder();
int year = calendar.GetYear(time);
int d;
do
{
d = year % 10;
sb.Insert(0, ChineseNumber[d]);
year = year / 10;
} while (year > 0);
return sb.ToString();
}
//月
public static string GetMonth(DateTime time)
{
int month = calendar.GetMonth(time);
int year = calendar.GetYear(time);
int leap = 0;
//正月不可能闰月
for (int i = 3; i <= month; i++)
{
if (calendar.IsLeapMonth(year, i))
{
leap = i;
break; //一年中最多有一个闰月
}
}
if (leap > 0) month--;
return (leap == month + 1 ? "闰" : "") + ChineseMonthName[month - 1];
}
//日
public static string GetDay(DateTime time)
{
return ChineseDayName[calendar.GetDayOfMonth(time) - 1];
}
//甲子记年法
public static string GetStemBranch(DateTime time)
{
int sexagenaryYear = calendar.GetSexagenaryYear(time);//与指定日期对应的甲子(60 年)循环中的年
string stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 - 1, 1);
return stemBranch;
}
//生肖
public static string GetSX(DateTime time)
{
int sexagenaryYear = calendar.GetSexagenaryYear(time);//与指定日期对应的甲子(60 年)循环中的年
string SX = ShuXiang.Substring(sexagenaryYear % 12 - 1, 1);
return SX;
}
private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
private static string ChineseNumber = "〇一二三四五六七八九";
public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
public const string ShuXiang = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
public static readonly string[] ChineseDayName = new string[] {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
}
转载于:https://www.cnblogs.com/zhaoyong/archive/2010/07/26/1785068.html
标签:Asp,string,万年历,System,aLabel,time,using,net,ChineseCalendarHelper 来源: https://blog.csdn.net/weixin_34259559/article/details/94541314