代码源于网络,自己整理的,一个C#资源分享平台,专业分享学习高质量代码,每周期布置学习任务,激发学习C#兴趣!(QQ群:128874886)
用法:
DateTime dt = mc.SelectionStart;
c = new EcanChineseCalendar(dt);
StringBuilder dayInfo = new StringBuilder();
dayInfo.Append("阳历:" + c.DateString + "\r\n");//阳历日期
dayInfo.Append("农历:" + c.ChineseDateString + "\r\n");//农历日期
dayInfo.Append("星期:" + c.WeekDayStr);//星期
dayInfo.Append("时辰:" + c.ChineseHour + "\r\n");//时辰
dayInfo.Append("属相:" + c.AnimalString + "\r\n");//属相
dayInfo.Append("节气:" + c.ChineseTwentyFourDay + "\r\n");//节气
dayInfo.Append("前一个节气:" + c.ChineseTwentyFourPrevDay + "\r\n");//前一个节气
dayInfo.Append("下一个节气:" + c.ChineseTwentyFourNextDay + "\r\n");//下一个节气
dayInfo.Append("节日:" + c.DateHoliday + "\r\n");//节日
dayInfo.Append("干支:" + c.GanZhiDateString + "\r\n");//干支
dayInfo.Append("星宿:" + c.ChineseConstellation + "\r\n");//星宿
dayInfo.Append("星座:" + c.Constellation + "\r\n");//星座
c = new EcanChineseCalendar(dt);
StringBuilder dayInfo = new StringBuilder();
dayInfo.Append("阳历:" + c.DateString + "\r\n");//阳历日期
dayInfo.Append("农历:" + c.ChineseDateString + "\r\n");//农历日期
dayInfo.Append("星期:" + c.WeekDayStr);//星期
dayInfo.Append("时辰:" + c.ChineseHour + "\r\n");//时辰
dayInfo.Append("属相:" + c.AnimalString + "\r\n");//属相
dayInfo.Append("节气:" + c.ChineseTwentyFourDay + "\r\n");//节气
dayInfo.Append("前一个节气:" + c.ChineseTwentyFourPrevDay + "\r\n");//前一个节气
dayInfo.Append("下一个节气:" + c.ChineseTwentyFourNextDay + "\r\n");//下一个节气
dayInfo.Append("节日:" + c.DateHoliday + "\r\n");//节日
dayInfo.Append("干支:" + c.GanZhiDateString + "\r\n");//干支
dayInfo.Append("星宿:" + c.ChineseConstellation + "\r\n");//星宿
dayInfo.Append("星座:" + c.Constellation + "\r\n");//星座
//类名:EcanChineseCalendar
//作用:农历类
//作者:刘典武
//时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text; namespace Ecan
{
#region ChineseCalendarException
/// <summary>
/// 中国日历异常处理
/// </summary>
public class newCalendarException : System.Exception
{
public newCalendarException(string msg)
: base(msg)
{
}
}
{
#region 内部结构
private struct SolarHolidayStruct//阳历
{
public int Month;
public int Day;
public int Recess; //假期长度
public string HolidayName;
public SolarHolidayStruct(int month, int day, int recess, string name)
{
Month = month;
Day = day;
Recess = recess;
HolidayName = name;
}
}
{
public int Month;
public int Day;
public int Recess;
public string HolidayName;
{
Month = month;
Day = day;
Recess = recess;
HolidayName = name;
}
}
{
public int Month;
public int WeekAtMonth;
public int WeekDay;
public string HolidayName;
{
Month = month;
WeekAtMonth = weekAtMonth;
WeekDay = weekDay;
HolidayName = name;
}
}
#endregion
private DateTime _date;
private DateTime _datetime;
private int _cMonth;
private int _cDay;
private bool _cIsLeapMonth; //当月是否闰月
private bool _cIsLeapYear; //当年是否有闰月
#endregion
#region 基本常量
private const int MinYear = 1900;
private const int MaxYear = 2050;
private static DateTime MinDay = new DateTime(1900, 1, 30);
private static DateTime MaxDay = new DateTime(2049, 12, 31);
private const int GanZhiStartYear = 1864; //干支计算起始年
private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22);//起始日
private const string HZNum = "零一二三四五六七八九";
private const int AnimalStartYear = 1900; //1900年为鼠年
private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);//28星宿参考值,本日为角
#endregion
/// <summary>
/// 来源于网上的农历数据
/// </summary>
/// <remarks>
/// 数据结构如下,共使用17位数据
/// 第17位:表示闰月天数,0表示29天 1表示30天
/// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
/// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
///</remarks>
private static int[] LunarDateArray = new int[]{
0x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
0x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
0x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
0x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
0x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
0x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
0x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
0x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
0x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
0x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
0x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
0x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
0x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
0x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
0x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
0x14B63
};
private static string[] _constellationName =
{
"白羊座", "金牛座", "双子座",
"巨蟹座", "狮子座", "处女座",
"天秤座", "天蝎座", "射手座",
"摩羯座", "水瓶座", "双鱼座"
};
#endregion
private static string[] _lunarHolidayName =
{
"小寒", "大寒", "立春", "雨水",
"惊蛰", "春分", "清明", "谷雨",
"立夏", "小满", "芒种", "夏至",
"小暑", "大暑", "立秋", "处暑",
"白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪", "冬至"
};
#endregion
private static string[] _chineseConstellationName =
{
//四 五 六 日 一 二 三
"角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹",
"斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝",
"奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿",
"井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","轸水蚓"
};
#endregion
private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
private static int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
#endregion
private static string ganStr = "甲乙丙丁戊己庚辛壬癸";
private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥";
private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
private static string nStr1 = "日一二三四五六七八九";
private static string nStr2 = "初十廿卅";
private static string[] _monthString =
{
"出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"
};
#endregion
private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{
new SolarHolidayStruct(1, 1, 1, "元旦"),
new SolarHolidayStruct(2, 2, 0, "世界湿地日"),
new SolarHolidayStruct(2, 10, 0, "国际气象节"),
new SolarHolidayStruct(2, 14, 0, "情人节"),
new SolarHolidayStruct(3, 1, 0, "国际海豹日"),
new SolarHolidayStruct(3, 5, 0, "学雷锋纪念日"),
new SolarHolidayStruct(3, 8, 0, "妇女节"),
new SolarHolidayStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),
new SolarHolidayStruct(3, 14, 0, "国际警察日"),
new SolarHolidayStruct(3,
//作用:农历类
//作者:刘典武
//时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text; namespace Ecan
{
#region ChineseCalendarException
/// <summary>
/// 中国日历异常处理
/// </summary>
public class newCalendarException : System.Exception
{
public newCalendarException(string msg)
: base(msg)
{
}
}
#endregion
public class EcanChineseCalendar
{
#region 内部结构
private struct SolarHolidayStruct//阳历
{
public int Month;
public int Day;
public int Recess; //假期长度
public string HolidayName;
public SolarHolidayStruct(int month, int day, int recess, string name)
{
Month = month;
Day = day;
Recess = recess;
HolidayName = name;
}
}
private struct LunarHolidayStruct//农历
{
public int Month;
public int Day;
public int Recess;
public string HolidayName;
public LunarHolidayStruct(int month, int day, int recess, string name)
{
Month = month;
Day = day;
Recess = recess;
HolidayName = name;
}
}
private struct WeekHolidayStruct
{
public int Month;
public int WeekAtMonth;
public int WeekDay;
public string HolidayName;
public WeekHolidayStruct(int month, int weekAtMonth, int weekDay, string name)
{
Month = month;
WeekAtMonth = weekAtMonth;
WeekDay = weekDay;
HolidayName = name;
}
}
#endregion
#region 内部变量
private DateTime _date;
private DateTime _datetime;
private int _cYear;
private int _cMonth;
private int _cDay;
private bool _cIsLeapMonth; //当月是否闰月
private bool _cIsLeapYear; //当年是否有闰月
#endregion
#region 基础数据
#region 基本常量
private const int MinYear = 1900;
private const int MaxYear = 2050;
private static DateTime MinDay = new DateTime(1900, 1, 30);
private static DateTime MaxDay = new DateTime(2049, 12, 31);
private const int GanZhiStartYear = 1864; //干支计算起始年
private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22);//起始日
private const string HZNum = "零一二三四五六七八九";
private const int AnimalStartYear = 1900; //1900年为鼠年
private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);//28星宿参考值,本日为角
#endregion
#region 阴历数据
/// <summary>
/// 来源于网上的农历数据
/// </summary>
/// <remarks>
/// 数据结构如下,共使用17位数据
/// 第17位:表示闰月天数,0表示29天 1表示30天
/// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
/// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
///</remarks>
private static int[] LunarDateArray = new int[]{
0x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
0x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
0x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
0x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
0x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
0x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
0x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
0x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
0x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
0x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
0x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
0x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
0x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
0x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
0x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
0x14B63
};
#endregion
#region 星座名称
private static string[] _constellationName =
{
"白羊座", "金牛座", "双子座",
"巨蟹座", "狮子座", "处女座",
"天秤座", "天蝎座", "射手座",
"摩羯座", "水瓶座", "双鱼座"
};
#endregion
#region 二十四节气
private static string[] _lunarHolidayName =
{
"小寒", "大寒", "立春", "雨水",
"惊蛰", "春分", "清明", "谷雨",
"立夏", "小满", "芒种", "夏至",
"小暑", "大暑", "立秋", "处暑",
"白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪", "冬至"
};
#endregion
#region 二十八星宿
private static string[] _chineseConstellationName =
{
//四 五 六 日 一 二 三
"角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹",
"斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝",
"奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿",
"井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","轸水蚓"
};
#endregion
#region 节气数据
private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
private static int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
#endregion
#region 农历相关数据
private static string ganStr = "甲乙丙丁戊己庚辛壬癸";
private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥";
private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
private static string nStr1 = "日一二三四五六七八九";
private static string nStr2 = "初十廿卅";
private static string[] _monthString =
{
"出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"
};
#endregion
#region 按公历计算的节日
private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{
new SolarHolidayStruct(1, 1, 1, "元旦"),
new SolarHolidayStruct(2, 2, 0, "世界湿地日"),
new SolarHolidayStruct(2, 10, 0, "国际气象节"),
new SolarHolidayStruct(2, 14, 0, "情人节"),
new SolarHolidayStruct(3, 1, 0, "国际海豹日"),
new SolarHolidayStruct(3, 5, 0, "学雷锋纪念日"),
new SolarHolidayStruct(3, 8, 0, "妇女节"),
new SolarHolidayStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),
new SolarHolidayStruct(3, 14, 0, "国际警察日"),
new SolarHolidayStruct(3,