C# 实现万年历示例:农历查询、干支、节气与星座计算
本文详细介绍如何使用 C# 编写一个功能完备的万年历类。该代码方案利用预置的数据表与位运算技术,实现了从 1900 年至 2049 年间公历与农历的精准互转,并能自动计算出干支(天干地支)、生肖、二十八星宿、星座、二十四节气时间点以及中西方节日。
核心原理与数据结构
C# 实现的万年历通常不通过网络实时获取,而是内置核心算法与数据表。本示例采用基于二进制位映射的存储方式,以极高的压缩率保存了数十年的农历信息:
- 阴历数据数组 (
LunarDateArray):这是一个整数数组。对于每一年,其对应的整数的二进制结构如下:
- 第 17 位:表示当年闰月的天数(0 代表 29 天,1 代表 30 天)。
- 第 16 至 5 位(共 12 位):按顺序表示农历正月至腊月的天数,1 为 30 天,0 为 29 天。
- 第 4 至 1 位:表示该年份的闰月是哪一个月(若无闰月则为 0)。
- 节气数据 (
sTermInfo):采用“定气”算法,依据地球在黄道上的位置计算。通过偏移量累加分钟数,从而得出每个节气的具体公历日期与时刻。 - 干支与生肖**>:以特定起始点(如甲子年或特定参考日)为基准,通过取余运算推导出具体的干支序号与汉字映射。
完整源代码实现
以下是完整的 ChineseCalendar 类定义,包含了异常处理、内部数据结构、构造函数及各类属性计算方法。请将其放入你的项目中。
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace yangliToyinli
{
/// <summary>
/// 中国日历异常处理
/// </summary>
public class ChineseCalendarException : System.Exception
{
public ChineseCalendarException(string msg) : base(msg) { }
}
/// <summary>
/// 中国农历类 版本V1.0 支持 1900.1.31日起至 2049.12.31日止的数据
/// </summary>
public class ChineseCalendar
{
#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, _cMonth, _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;
private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);
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 = { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };
private static string[] _lunarHolidayName = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
private static string[] _chineseConstellationName = { "角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹","斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝","奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿","井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","轸水蚓" };
private static string[] SolarTerm = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
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 };
private static string ganStr = "甲乙丙丁戊己庚辛壬癸";
private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥";
private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
private static string nStr1 = "日一二三四五六七八九";
private static string nStr2 = "初十廿卅";
private static string[] _monthString = { "出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月" };
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, 15, 0, "消费者权益日"),
new SolarHolidayStruct(3, 17, 0, "中国国医节 国际航海日"), new SolarHolidayStruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日"),
new SolarHolidayStruct(3, 22, 0, "世界水日"), new SolarHolidayStruct(3, 24, 0, "世界防治结核病日"),
new SolarHolidayStruct(4, 1, 0, "愚人节"), new SolarHolidayStruct(4, 7, 0, "世界卫生日"),
new SolarHolidayStruct(4, 22, 0, "世界地球日"), new SolarHolidayStruct(5, 1, 1, "劳动节"),
new SolarHolidayStruct(5, 2, 1, "劳动节假日"), new SolarHolidayStruct(5, 3, 1, "劳动节假日"),
new SolarHolidayStruct(5, 4, 0, "青年节"), new SolarHolidayStruct(5, 8, 0, "世界红十字日"),
new SolarHolidayStruct(5, 12, 0, "国际护士节"), new SolarHolidayStruct(5, 31, 0, "世界无烟日"),
new SolarHolidayStruct(6, 1, 0, "国际儿童节"), new SolarHolidayStruct(6, 5, 0, "世界环境保护日"),
new SolarHolidayStruct(6, 26, 0, "国际禁毒日"), new SolarHolidayStruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日"),
new SolarHolidayStruct(7, 11, 0, "世界人口日"), new SolarHolidayStruct(8, 1, 0, "建军节"),
new SolarHolidayStruct(8, 8, 0, "中国男子节 父亲节"), new SolarHolidayStruct(8, 15, 0, "抗日战争胜利纪念"),
new SolarHolidayStruct(9, 9, 0, "逝世纪念"), new SolarHolidayStruct(9, 10, 0, "教师节"),
new SolarHolidayStruct(9, 18, 0, "九·一八事变纪念日"), new SolarHolidayStruct(9, 20, 0, "国际爱牙日"),
new SolarHolidayStruct(9, 27, 0, "世界旅游日"), new SolarHolidayStruct(9, 28, 0, "孔子诞辰"),
new SolarHolidayStruct(10, 1, 1, "国庆节 国际音乐日"), new SolarHolidayStruct(10, 2, 1, "国庆节假日"),
new SolarHolidayStruct(10, 3, 1, "国庆节假日"), new SolarHolidayStruct(10, 6, 0, "老人节"),
new SolarHolidayStruct(10, 24, 0, "联合国日"), new SolarHolidayStruct(11, 10, 0, "世界青年节"),
new SolarHolidayStruct(11, 12, 0, "孙中山诞辰纪念"), new SolarHolidayStruct(12, 1, 0, "世界艾滋病日"),
new SolarHolidayStruct(12, 3, 0, "世界残疾人日"), new SolarHolidayStruct(12, 20, 0, "澳门回归纪念"),
new SolarHolidayStruct(12, 24, 0, "平安夜"), new SolarHolidayStruct(12, 25, 0, "圣诞节"),
new SolarHolidayStruct(12, 26, 0, " 诞辰纪念")
};
private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
new LunarHolidayStruct(1, 1, 1, "春节"), new LunarHolidayStruct(1, 15, 0, "元宵节"),
new LunarHolidayStruct(5, 5, 0, "端午节"), new LunarHolidayStruct(7, 7, 0, "七夕情人节"),
new LunarHolidayStruct(7, 15, 0, "中元节 盂兰盆节"), new LunarHolidayStruct(8, 15, 0, "中秋节"),
new LunarHolidayStruct(9, 9, 0, "重阳节"), new LunarHolidayStruct(12, 8, 0, "腊八节"),
new LunarHolidayStruct(12, 23, 0, "北方小年(扫房)"), new LunarHolidayStruct(12, 24, 0, "南方小年(掸尘)")
};
private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{
new WeekHolidayStruct(5, 2, 1, "母亲节"), new WeekHolidayStruct(5, 3, 1, "全国助残日"),
new WeekHolidayStruct(6, 3, 1, "父亲节"), new WeekHolidayStruct(9, 3, 3, "国际和平日"),
new WeekHolidayStruct(9, 4, 1, "国际聋人节"), new WeekHolidayStruct(10, 1, 2, "国际住房日"),
new WeekHolidayStruct(10, 1, 4, "国际减轻自然灾害日"), new WeekHolidayStruct(11, 4, 5, "感恩节")
};
#endregion
#region 构造函数
public ChineseCalendar(DateTime dt)
{
CheckDateLimit(dt);
_date = dt.Date; _datetime = dt;
int leap = 0, temp = 0, offset;
TimeSpan ts = _date - ChineseCalendar.MinDay;
offset = ts.Days;
for (int i = MinYear; i <= MaxYear; i++)
{
temp = GetChineseYearDays(i);
if (offset - temp < 1) break;
else offset = offset - temp;
}
_cYear = i;
leap = GetChineseLeapMonth(_cYear);
_cIsLeapYear = (leap > 0);
_cIsLeapMonth = false;
for (int i = 1; i <= 12; i++)
{
if ((leap > 0) && (i == leap + 1) && (_cIsLeapMonth == false))
{
_cIsLeapMonth = true; i = i - 1;
temp = GetChineseLeapMonthDays(_cYear);
}
else
{
_cIsLeapMonth = false;
temp = GetChineseMonthDays(_cYear, i);
}
offset = offset - temp;
if (offset <= 0) break;
}
offset = offset + temp;
_cMonth = i; _cDay = offset;
}
public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
{
CheckChineseDateLimit(cy, cm, cd, leapMonthFlag);
_cYear = cy; _cMonth = cm; _cDay = cd;
int offset = 0;
for (int i = MinYear; i < cy; i++) offset += GetChineseYearDays(i);
int leap = GetChineseLeapMonth(cy);
_cIsLeapYear = (leap != 0);
if (cm != leap) _cIsLeapMonth = false;
else _cIsLeapMonth = leapMonthFlag;
if ((_cIsLeapYear == false) || (cm < leap))
{
for (int i = 1; i < cm; i++) offset += GetChineseMonthDays(cy, i);
if (cd > GetChineseMonthDays(cy, cm)) throw new ChineseCalendarException("不合法的农历日期");
offset += cd;
}
else
{
for (int i = 1; i < cm; i++) offset += GetChineseMonthDays(cy, i);
if (cm > leap)
{
offset += GetChineseLeapMonthDays(cy);
if (cd > GetChineseMonthDays(cy, cm)) throw new ChineseCalendarException("不合法的农历日期");
offset += cd;
}
else
{
if (_cIsLeapMonth) offset += GetChineseMonthDays(cy, cm);
if (cd > GetChineseLeapMonthDays(cy)) throw new ChineseCalendarException("不合法的农历日期");
offset += cd;
}
}
_date = MinDay.AddDays(offset);
}
#endregion
#region 私有函数
private int GetChineseMonthDays(int year, int month)
{
return BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), (16 - month)) ? 30 : 29;
}
private int GetChineseLeapMonth(int year) { return LunarDateArray[year - MinYear] & 0xF; }
private int GetChineseLeapMonthDays(int year)
{
if (GetChineseLeapMonth(year) == 0) return 0;
return (LunarDateArray[year - MinYear] & 0x10000) != 0 ? 30 : 29;
}
private int GetChineseYearDays(int year)
{
int sumDay = 348, info = LunarDateArray[year - MinYear] & 0x0FFFF;
for (int m = 0; m < 12; m++) if ((info & (1 << (11 - m))) != 0) sumDay++;
return sumDay + GetChineseLeapMonthDays(year);
}
private string GetChineseHour(DateTime dt)
{
int _hour = dt.Hour, _minute = dt.Minute; if (_minute != 0) _hour++; if (_hour >= 12) _hour = 0;
TimeSpan ts = this._date - GanZhiStartDay; int i = ts.Days % 60;
int indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1;
string tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(0, indexGan + 2);
return tmpGan[_hour / 2].ToString() + zhiStr[_hour / 2].ToString();
}
private void CheckDateLimit(DateTime dt)
{
if (dt < MinDay || dt > MaxDay) throw new ChineseCalendarException("超出可转换的日期");
}
private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
{
if (year < MinYear || year > MaxYear) throw new ChineseCalendarException("非法农历日期");
if (month < 1 || month > 12 || day < 1 || day > 30) throw new ChineseCalendarException("非法农历日期");
int leap = GetChineseLeapMonth(year);
if (leapMonth && month != leap) throw new ChineseCalendarException("非法农历日期");
}
private string ConvertNumToChineseNum(char n)
{
if (n < '0' || n > '9') return "";
switch (n) { case '0': return HZNum[0].ToString(); case '1': return HZNum[1].ToString(); case '2': return HZNum[2].ToString();
case '3': return HZNum[3].ToString(); case '4': return HZNum[4].ToString(); case '5': return HZNum[5].ToString();
case '6': return HZNum[6].ToString(); case '7': return HZNum[7].ToString(); case '8': return HZNum[8].ToString(); default: return HZNum[9].ToString(); }
}
private bool BitTest32(int num, int bitpostion)
{
if (bitpostion < 0 || bitpostion > 31) throw new Exception("Error Param: bitpostion[0-31]" + bitpostion); num;
return (num & (1 << bitpostion)) != 0;
}
private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
{
switch (dayOfWeek) { case DayOfWeek.Sunday: return 1; case DayOfWeek.Monday: return 2; case DayOfWeek.Tuesday: return 3;
case DayOfWeek.Wednesday: return 4; case DayOfWeek.Thursday: return 5; case DayOfWeek.Friday: return 6; default: return 7; }
}
private bool CompareWeekDayHoliday(DateTime date, int month, int week, int day)
{
if (date.Month != month) return false;
if (ConvertDayOfWeek(date.DayOfWeek) != day) return false;
DateTime firstDay = new DateTime(date.Year, date.Month, 1);
int firWeekDays = 7 - ConvertDayOfWeek(firstDay.DayOfWeek) + 1;
if (ConvertDayOfWeek(firstDay.DayOfWeek) > day)
return (week - 1) * 7 + day + firWeekDays == date.Day;
else
return day + firWeekDays + (week - 2) * 7 == date.Day;
}
#endregion
#region 属性:节日
public string ChineseCalendarHoliday
{
get
{
string tempStr = "";
if (!_cIsLeapMonth)
{
foreach (var lh in lHolidayInfo) if (lh.Month == _cMonth && lh.Day == _cDay) { tempStr = lh.HolidayName; break; }
if (_cMonth == 12 && _cDay == GetChineseMonthDays(_cYear, 12)) tempStr = "除夕";
}
return tempStr;
}
}
public string WeekDayHoliday
{
get
{
string tempStr = "";
foreach (var wh in wHolidayInfo) if (CompareWeekDayHoliday(_date, wh.Month, wh.WeekAtMonth, wh.WeekDay)) { tempStr = wh.HolidayName; break; }
return tempStr;
}
}
public string DateHoliday
{
get
{
string tempStr = "";
foreach (var sh in sHolidayInfo) if (sh.Month == _date.Month && sh.Day == _date.Day) { tempStr = sh.HolidayName; break; }
return tempStr;
}
}
#endregion
#region 属性:公历
public DateTime Date { get { return _date; } set { _date = value; } }
public DayOfWeek WeekDay { get { return _date.DayOfWeek; } }
public string WeekDayStr
{
get
{
switch (_date.DayOfWeek) { case DayOfWeek.Sunday: return "星期日"; case DayOfWeek.Monday: return "星期一"; case DayOfWeek.Tuesday: return "星期二";
case DayOfWeek.Wednesday: return "星期三"; case DayOfWeek.Thursday: return "星期四"; case DayOfWeek.Friday: return "星期五"; default: return "星期六"; }
}
}
public string DateString { get { return "公元" + _date.ToLongDateString(); } }
public bool IsLeapYear { get { return DateTime.IsLeapYear(_date.Year); } }
public string ChineseConstellation
{
get
{
int modStarDay = ((this._date - ChineseConstellationReferDay).Days) % 28;
return modStarDay >= 0 ? _chineseConstellationName[modStarDay] : _chineseConstellationName[27 + modStarDay];
}
}
public string ChineseHour { get { return GetChineseHour(_datetime); } }
#endregion
#region 属性:农历
public bool IsChineseLeapMonth { get { return _cIsLeapMonth; } }
public bool IsChineseLeapYear { get { return _cIsLeapYear; } }
public int ChineseDay { get { return _cDay; } }
public string ChineseDayString
{
get
{
switch (_cDay) { case 10: return "初十"; case 20: return "二十"; case 30: return "三十"; default: return nStr2[(int)(_cDay / 10)].ToString() + nStr1[_cDay % 10].ToString(); }
}
}
public int ChineseMonth { get { return _cMonth; } }
public string ChineseMonthString { get { return _monthString[_cMonth]; } }
public int ChineseYear { get { return _cYear; } }
public string ChineseYearString
{
get
{
string tempStr = ""; string num = _cYear.ToString();
for (int i = 0; i < 4; i++) tempStr += ConvertNumToChineseNum(num[i]);
return tempStr + "年";
}
}
public string ChineseDateString
{
get
{
if (_cIsLeapMonth) return "农历" + ChineseYearString + "闰" + ChineseMonthString + ChineseDayString;
return "农历" + ChineseYearString + ChineseMonthString + ChineseDayString;
}
}
public string ChineseTwentyFourDay
{
get
{
DateTime newDate; double num; int y = _date.Year;
for (int i = 1; i <= 24; i++)
{
num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
newDate = new DateTime(1900, 1, 6, 2, 5, 0).AddMinutes(num);
if (newDate.DayOfYear == _date.DayOfYear) return SolarTerm[i - 1];
}
return "";
}
}
public string ChineseTwentyFourPrevDay
{
get
{
DateTime newDate; double num; int y = _date.Year;
for (int i = 24; i >= 1; i--)
{
num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
newDate = new DateTime(1900, 1, 6, 2, 5, 0).AddMinutes(num);
if (newDate.DayOfYear < _date.DayOfYear) return SolarTerm[i - 1] + "[" + newDate.ToString("yyyy-MM-dd") + "]";
}
return "";
}
}
public string ChineseTwentyFourNextDay
{
get
{
DateTime newDate; double num; int y = _date.Year;
for (int i = 1; i <= 24; i++)
{
num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
newDate = new DateTime(1900, 1, 6, 2, 5, 0).AddMinutes(num);
if (newDate.DayOfYear > _date.DayOfYear) return SolarTerm[i - 1] + "[" + newDate.ToString("yyyy-MM-dd") + "]";
}
return "";
}
}
#endregion
#region 属性:星座、属相、干支
public string Constellation
{
get
{
int index = 0, y = _date.Month * 100 + _date.Day;
if (y >= 321 && y <= 419) index = 0; else if (y >= 420 && y <= 520) index = 1; else if (y >= 521 && y <= 620) index = 2;
else if (y >= 621 && y <= 722) index = 3; else if (y >= 723 && y <= 822) index = 4; else if (y >= 823 && y <= 922) index = 5;
else if (y >= 923 && y <= 1022) index = 6; else if (y >= 1023 && y <= 1121) index = 7; else if (y >= 1122 && y <= 1221) index = 8;
else if (y >= 1222 || y <= 119) index = 9; else if (y >= 120 && y <= 218) index = 10; else if (y >= 219 && y <= 320) index = 11;
return _constellationName[index];
}
}
public string AnimalString
{
get
{
int offset = _date.Year - AnimalStartYear;
return animalStr[offset % 12].ToString();
}
}
public string GanZhiYearString
{
get
{
int i = (_cYear - GanZhiStartYear) % 60;
return ganStr[i % 10].ToString() + zhiStr[i % 12].ToString() + "年";
}
}
public string GanZhiMonthString
{
get
{
int zhiIndex = _cMonth > 10 ? _cMonth - 10 : _cMonth + 2;
string zhi = zhiStr[zhiIndex - 1].ToString();
int i = (_cYear - GanZhiStartYear) % 60, ganIndex = 1;
switch (i % 10)
{
case 0: ganIndex = 3; break; case 1: ganIndex = 5; break; case 2: ganIndex = 7; break;
case 3: ganIndex = 9; break; case 4: ganIndex = 1; break; case 5: ganIndex = 3; break;
case 6: ganIndex = 5; break; case 7: ganIndex = 7; break; case 8: ganIndex = 9; break; case 9: ganIndex = 1; break;
}
return ganStr[(ganIndex + _cMonth - 2) % 10].ToString() + zhi + "月";
}
}
public string GanZhiDayString
{
get
{
int offset = (_date - GanZhiStartDay).Days % 60;
return ganStr[offset % 10].ToString() + zhiStr[offset % 12].ToString() + "日";
}
}
public string GanZhiDateString { get { return GanZhiYearString + GanZhiMonthString + GanZhiDayString; } }
#endregion
#region 方法
public ChineseCalendar NextDay() { return new ChineseCalendar(_date.AddDays(1)); }
public ChineseCalendar PrevDay() { return new ChineseCalendar(_date.AddDays(-1)); }
#endregion
}
}
调用方式与运行结果
在实际项目中,你可以通过实例化 ChineseCalendar 对象并传入当前的 DateTime 来获取详细信息。以下是一个简单的控制台程序调用示例:
复制代码 代码如下:
DateTime dt = DateTime.Now;
ChineseCalendar cc = new ChineseCalendar(dt);
Console.WriteLine("阳历:" + cc.DateString);
Console.WriteLine("属相:" + cc.AnimalString);
Console.WriteLine("农历:" + cc.ChineseDateString);
Console.WriteLine("时辰:" + cc.ChineseHour);
Console.WriteLine("节气:" + cc.ChineseTwentyFourDay);
Console.WriteLine("节日:" + cc.DateHoliday);
Console.WriteLine("前一个节气:" + cc.ChineseTwentyFourPrevDay);
Console.WriteLine("后一个节气:" + cc.ChineseTwentyFourNextDay);
Console.WriteLine("干支:" + cc.GanZhiDateString);
Console.WriteLine("星期:" + cc.WeekDayStr);
Console.WriteLine("星宿:" + cc.ChineseConstellation);
Console.WriteLine("星座:" + cc.Constellation);
执行上述代码后,将获得类似以下的结构化输出,涵盖了从公历到农历的全面对照:
复制代码 代码如下:
阳历:公元2013年1月27日
属相:蛇
农历:农历二零一二年腊月十六
时辰:庚申
节气:
节日:
前一个节气:大寒[2013-01-20]
后一个节气:立春[2013-02-03]
干支:壬辰年癸丑月癸巳日
星期:星期日
星宿:房日兔
星座:水瓶座
总结
以上即为使用 C# 实现万年历及农历查询的详细操作过程。该实现方案无需外部依赖库,完全依靠本地计算与内置表,具有高效、准确的特性。更多关于 C# 时间处理与日期开发的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













