C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰等,代码如下:
using System.Collections.Generic;
using System.Text; using System; namespace yangliToyinli
{
#region ChineseCalendarException
/// <summary>
/// 中国日历异常处理
/// </summary>
public class ChineseCalendarException : System.Exception
{
public ChineseCalendarException(string msg)
: base(msg)
{
}
} #endregion /// <summary>
/// 中国农历类 版本V1.0 支持 1900.1.31日起至 2049.12.31日止的数据
/// </summary>
/// <remarks>
/// 本程序使用数据来源于网上的万年历查询,并综合了一些其它数据
/// </remarks>
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;
private int _cMonth;
private int _cDay;
private bool _cIsLeapMonth; //当月是否闰月
private bool _cIsLeapYear; //当年是否有闰月
#endregion #region 基础数据
#region 基本常量
private const int MinYear = ;
private const int MaxYear = ;
private static DateTime MinDay = new DateTime(, , );
private static DateTime MaxDay = new DateTime(, , );
private const int GanZhiStartYear = ; //干支计算起始年
private static DateTime GanZhiStartDay = new DateTime(, , );//起始日
private const string HZNum = "零一二三四五六七八九";
private const int AnimalStartYear = ; //1900年为鼠年
private static DateTime ChineseConstellationReferDay = new DateTime(, , );//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[]{
x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
x14B63
}; #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[] { , , , , , , , , , , , , , , , , , , , , , , , };
#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(, , , "元旦"),
new SolarHolidayStruct(, , , "世界湿地日"),
new SolarHolidayStruct(, , , "国际气象节"),
new SolarHolidayStruct(, , , "情人节"),
new SolarHolidayStruct(, , , "国际海豹日"),
new SolarHolidayStruct(, , , "学雷锋纪念日"),
new SolarHolidayStruct(, , , "妇女节"),
new SolarHolidayStruct(, , , "植树节 孙中山逝世纪念日"),
new SolarHolidayStruct(, , , "国际警察日"),
new SolarHolidayStruct(, , , "消费者权益日"),
new SolarHolidayStruct(, , , "中国国医节 国际航海日"),
new SolarHolidayStruct(, , , "世界森林日 消除种族歧视国际日 世界儿歌日"),
new SolarHolidayStruct(, , , "世界水日"),
new SolarHolidayStruct(, , , "世界防治结核病日"),
new SolarHolidayStruct(, , , "愚人节"),
new SolarHolidayStruct(, , , "世界卫生日"),
new SolarHolidayStruct(, , , "世界地球日"),
new SolarHolidayStruct(, , , "劳动节"),
new SolarHolidayStruct(, , , "劳动节假日"),
new SolarHolidayStruct(, , , "劳动节假日"),
new SolarHolidayStruct(, , , "青年节"),
new SolarHolidayStruct(, , , "世界红十字日"),
new SolarHolidayStruct(, , , "国际护士节"),
new SolarHolidayStruct(, , , "世界无烟日"),
new SolarHolidayStruct(, , , "国际儿童节"),
new SolarHolidayStruct(, , , "世界环境保护日"),
new SolarHolidayStruct(, , , "国际禁毒日"),
new SolarHolidayStruct(, , , "建党节 香港回归纪念 世界建筑日"),
new SolarHolidayStruct(, , , "世界人口日"),
new SolarHolidayStruct(, , , "建军节"),
new SolarHolidayStruct(, , , "中国男子节 父亲节"),
new SolarHolidayStruct(, , , "抗日战争胜利纪念"),
new SolarHolidayStruct(, , , " 逝世纪念"),
new SolarHolidayStruct(, , , "教师节"),
new SolarHolidayStruct(, , , "九·一八事变纪念日"),
new SolarHolidayStruct(, , , "国际爱牙日"),
new SolarHolidayStruct(, , , "世界旅游日"),
new SolarHolidayStruct(, , , "孔子诞辰"),
new SolarHolidayStruct(, , , "国庆节 国际音乐日"),
new SolarHolidayStruct(, , , "国庆节假日"),
new SolarHolidayStruct(, , , "国庆节假日"),
new SolarHolidayStruct(, , , "老人节"),
new SolarHolidayStruct(, , , "联合国日"),
new SolarHolidayStruct(, , , "世界青年节"),
new SolarHolidayStruct(, , , "孙中山诞辰纪念"),
new SolarHolidayStruct(, , , "世界艾滋病日"),
new SolarHolidayStruct(, , , "世界残疾人日"),
new SolarHolidayStruct(, , , "澳门回归纪念"),
new SolarHolidayStruct(, , , "平安夜"),
new SolarHolidayStruct(, , , "圣诞节"),
new SolarHolidayStruct(, , , " 诞辰纪念")
};
#endregion #region 按农历计算的节日
private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
new LunarHolidayStruct(, , , "春节"),
new LunarHolidayStruct(, , , "元宵节"),
new LunarHolidayStruct(, , , "端午节"),
new LunarHolidayStruct(, , , "七夕情人节"),
new LunarHolidayStruct(, , , "中元节 盂兰盆节"),
new LunarHolidayStruct(, , , "中秋节"),
new LunarHolidayStruct(, , , "重阳节"),
new LunarHolidayStruct(, , , "腊八节"),
new LunarHolidayStruct(, , , "北方小年(扫房)"),
new LunarHolidayStruct(, , , "南方小年(掸尘)"),
//new LunarHolidayStruct(12, 30, 0, "除夕") //注意除夕需要其它方法进行计算
};
#endregion #region 按某月第几个星期几
private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{
new WeekHolidayStruct(, , , "母亲节"),
new WeekHolidayStruct(, , , "全国助残日"),
new WeekHolidayStruct(, , , "父亲节"),
new WeekHolidayStruct(, , , "国际和平日"),
new WeekHolidayStruct(, , , "国际聋人节"),
new WeekHolidayStruct(, , , "国际住房日"),
new WeekHolidayStruct(, , , "国际减轻自然灾害日"),
new WeekHolidayStruct(, , , "感恩节")
};
#endregion #endregion #region 构造函数
#region ChinaCalendar <公历日期初始化>
/// <summary>
/// 用一个标准的公历日期来初使化
/// </summary>
/// <param name="dt"></param>
public ChineseCalendar(DateTime dt)
{
int i;
int leap;
int temp;
int offset; CheckDateLimit(dt); _date = dt.Date;
_datetime = dt; //农历日期计算部分
leap = ;
temp = ; TimeSpan ts = _date - ChineseCalendar.MinDay;//计算两天的基本差距
offset = ts.Days; for (i = MinYear; i <= MaxYear; i++)
{
temp = GetChineseYearDays(i); //求当年农历年天数
if (offset - temp < )
break;
else
{
offset = offset - temp;
}
}
_cYear = i; leap = GetChineseLeapMonth(_cYear);//计算该年闰哪个月
//设定当年是否有闰月
if (leap > )
{
_cIsLeapYear = true;
}
else
{
_cIsLeapYear = false;
} _cIsLeapMonth = false;
for (i = ; i <= ; i++)
{
//闰月
if ((leap > ) && (i == leap + ) && (_cIsLeapMonth == false))
{
_cIsLeapMonth = true;
i = i - ;
temp = GetChineseLeapMonthDays(_cYear); //计算闰月天数
}
else
{
_cIsLeapMonth = false;
temp = GetChineseMonthDays(_cYear, i);//计算非闰月天数
} offset = offset - temp;
if (offset <= ) break;
} offset = offset + temp;
_cMonth = i;
_cDay = offset;
}
#endregion #region ChinaCalendar <农历日期初始化>
/// <summary>
/// 用农历的日期来初使化
/// </summary>
/// <param name="cy">农历年</param>
/// <param name="cm">农历月</param>
/// <param name="cd">农历日</param>
/// <param name="LeapFlag">闰月标志</param>
public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
{
int i, leap, Temp, offset; CheckChineseDateLimit(cy, cm, cd, leapMonthFlag); _cYear = cy;
_cMonth = cm;
_cDay = cd; offset = ; for (i = MinYear; i < cy; i++)
{
Temp = GetChineseYearDays(i); //求当年农历年天数
offset = offset + Temp;
} leap = GetChineseLeapMonth(cy);// 计算该年应该闰哪个月
if (leap != )
{
this._cIsLeapYear = true;
}
else
{
this._cIsLeapYear = false;
} if (cm != leap)
{
_cIsLeapMonth = false; //当前日期并非闰月
}
else
{
_cIsLeapMonth = leapMonthFlag; //使用用户输入的是否闰月月份
} if ((_cIsLeapYear == false) || //当年没有闰月
(cm < leap)) //计算月份小于闰月
{
#region ...
for (i = ; i < cm; i++)
{
Temp = GetChineseMonthDays(cy, i);//计算非闰月天数
offset = offset + Temp;
} //检查日期是否大于最大天
if (cd > GetChineseMonthDays(cy, cm))
{
throw new ChineseCalendarException("不合法的农历日期");
}
offset = offset + cd; //加上当月的天数
#endregion
}
else //是闰年,且计算月份大于或等于闰月
{
#region ...
for (i = ; i < cm; i++)
{
Temp = GetChineseMonthDays(cy, i); //计算非闰月天数
offset = offset + Temp;
} if (cm > leap) //计算月大于闰月
{
Temp = GetChineseLeapMonthDays(cy); //计算闰月天数
offset = offset + Temp; //加上闰月天数 if (cd > GetChineseMonthDays(cy, cm))
{
throw new ChineseCalendarException("不合法的农历日期");
}
offset = offset + cd;
}
else //计算月等于闰月
{
//如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数
if (this._cIsLeapMonth == true) //计算月为闰月
{
Temp = GetChineseMonthDays(cy, cm); //计算非闰月天数
offset = offset + Temp;
} if (cd > GetChineseLeapMonthDays(cy))
{
throw new ChineseCalendarException("不合法的农历日期");
}
offset = offset + cd;
}
#endregion
} _date = MinDay.AddDays(offset);
}
#endregion
#endregion #region 私有函数 #region GetChineseMonthDays
//传回农历 y年m月的总天数
private int GetChineseMonthDays(int year, int month)
{
if (BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), ( - month)))
{
return ;
}
else
{
return ;
}
}
#endregion #region GetChineseLeapMonth
//传回农历 y年闰哪个月 1-12 , 没闰传回 0
private int GetChineseLeapMonth(int year)
{ return LunarDateArray[year - MinYear] & 0xF; }
#endregion #region GetChineseLeapMonthDays
//传回农历 y年闰月的天数
private int GetChineseLeapMonthDays(int year)
{
if (GetChineseLeapMonth(year) != )
{
if ((LunarDateArray[year - MinYear] & 0x10000) != )
{
return ;
}
else
{
return ;
}
}
else
{
return ;
}
}
#endregion #region GetChineseYearDays
/// <summary>
/// 取农历年一年的天数
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
private int GetChineseYearDays(int year)
{
int i, f, sumDay, info; sumDay = ; //29天 X 12个月
i = 0x8000;
info = LunarDateArray[year - MinYear] & 0x0FFFF; //计算12个月中有多少天为30天
for (int m = ; m < ; m++)
{
f = info & i;
if (f != )
{
sumDay++;
}
i = i >> ;
}
return sumDay + GetChineseLeapMonthDays(year);
}
#endregion #region GetChineseHour
/// <summary>
/// 获得当前时间的时辰
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
///
private string GetChineseHour(DateTime dt)
{ int _hour, _minute, offset, i;
int indexGan;
string ganHour, zhiHour;
string tmpGan; //计算时辰的地支
_hour = dt.Hour; //获得当前时间小时
_minute = dt.Minute; //获得当前时间分钟 if (_minute != ) _hour += ;
offset = _hour / ;
if (offset >= ) offset = ;
//zhiHour = zhiStr[offset].ToString(); //计算天干
TimeSpan ts = this._date - GanZhiStartDay;
i = ts.Days % ; indexGan = ((i % + ) * - ) % - ; //ganStr[i % 10] 为日的天干,(n*2-1) %10得出地支对应,n从1开始
tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(, indexGan + );//凑齐12位
//ganHour = ganStr[((i % 10 + 1) * 2 - 1) % 10 - 1].ToString(); return tmpGan[offset].ToString() + zhiStr[offset].ToString(); }
#endregion #region CheckDateLimit
/// <summary>
/// 检查公历日期是否符合要求
/// </summary>
/// <param name="dt"></param>
private void CheckDateLimit(DateTime dt)
{
if ((dt < MinDay) || (dt > MaxDay))
{
throw new ChineseCalendarException("超出可转换的日期");
}
}
#endregion #region CheckChineseDateLimit
/// <summary>
/// 检查农历日期是否合理
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <param name="leapMonth"></param>
private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
{
if ((year < MinYear) || (year > MaxYear))
{
throw new ChineseCalendarException("非法农历日期");
}
if ((month < ) || (month > ))
{
throw new ChineseCalendarException("非法农历日期");
}
if ((day < ) || (day > )) //中国的月最多30天
{
throw new ChineseCalendarException("非法农历日期");
} int leap = GetChineseLeapMonth(year);// 计算该年应该闰哪个月
if ((leapMonth == true) && (month != leap))
{
throw new ChineseCalendarException("非法农历日期");
} }
#endregion #region ConvertNumToChineseNum
/// <summary>
/// 将0-9转成汉字形式
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
private string ConvertNumToChineseNum(char n)
{
if ((n < '') || (n > '')) return "";
switch (n)
{
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
case '':
return HZNum[].ToString();
default:
return "";
}
}
#endregion #region BitTest32
/// <summary>
/// 测试某位是否为真
/// </summary>
/// <param name="num"></param>
/// <param name="bitpostion"></param>
/// <returns></returns>
private bool BitTest32(int num, int bitpostion)
{ if ((bitpostion > ) || (bitpostion < ))
throw new Exception("Error Param: bitpostion[0-31]:" + bitpostion.ToString()); int bit = << bitpostion; if ((num & bit) == )
{
return false;
}
else
{
return true;
}
}
#endregion #region ConvertDayOfWeek
/// <summary>
/// 将星期几转成数字表示
/// </summary>
/// <param name="dayOfWeek"></param>
/// <returns></returns>
private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
{
switch (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 ;
case DayOfWeek.Saturday:
return ;
default:
return ;
}
}
#endregion #region CompareWeekDayHoliday
/// <summary>
/// 比较当天是不是指定的第周几
/// </summary>
/// <param name="date"></param>
/// <param name="month"></param>
/// <param name="week"></param>
/// <param name="day"></param>
/// <returns></returns>
private bool CompareWeekDayHoliday(DateTime date, int month, int week, int day)
{
bool ret = false; if (date.Month == month) //月份相同
{
if (ConvertDayOfWeek(date.DayOfWeek) == day) //星期几相同
{
DateTime firstDay = new DateTime(date.Year, date.Month, );//生成当月第一天
int i = ConvertDayOfWeek(firstDay.DayOfWeek);
int firWeekDays = - ConvertDayOfWeek(firstDay.DayOfWeek) + ; //计算第一周剩余天数 if (i > day)
{
if ((week - ) * + day + firWeekDays == date.Day)
{
ret = true;
}
}
else
{
if (day + firWeekDays + (week - ) * == date.Day)
{
ret = true;
}
}
}
} return ret;
}
#endregion
#endregion #region 属性 #region 节日
#region ChineseCalendarHoliday
/// <summary>
/// 计算中国农历节日
/// </summary>
public string ChineseCalendarHoliday
{
get
{
string tempStr = "";
if (this._cIsLeapMonth == false) //闰月不计算节日
{
foreach (LunarHolidayStruct lh in lHolidayInfo)
{
if ((lh.Month == this._cMonth) && (lh.Day == this._cDay))
{ tempStr = lh.HolidayName;
break; }
} //对除夕进行特别处理
if (this._cMonth == )
{
int i = GetChineseMonthDays(this._cYear, ); //计算当年农历12月的总天数
if (this._cDay == i) //如果为最后一天
{
tempStr = "除夕";
}
}
}
return tempStr;
}
}
#endregion #region WeekDayHoliday
/// <summary>
/// 按某月第几周第几日计算的节日
/// </summary>
public string WeekDayHoliday
{
get
{
string tempStr = "";
foreach (WeekHolidayStruct wh in wHolidayInfo)
{
if (CompareWeekDayHoliday(_date, wh.Month, wh.WeekAtMonth, wh.WeekDay))
{
tempStr = wh.HolidayName;
break;
}
}
return tempStr;
}
}
#endregion #region DateHoliday
/// <summary>
/// 按公历日计算的节日
/// </summary>
public string DateHoliday
{
get
{
string tempStr = ""; foreach (SolarHolidayStruct sh in sHolidayInfo)
{
if ((sh.Month == _date.Month) && (sh.Day == _date.Day))
{
tempStr = sh.HolidayName;
break;
}
}
return tempStr;
}
}
#endregion
#endregion #region 公历日期
#region Date
/// <summary>
/// 取对应的公历日期
/// </summary>
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
#endregion #region WeekDay
/// <summary>
/// 取星期几
/// </summary>
public DayOfWeek WeekDay
{
get { return _date.DayOfWeek; }
}
#endregion #region WeekDayStr
/// <summary>
/// 周几的字符
/// </summary>
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 "星期六";
}
}
}
#endregion #region DateString
/// <summary>
/// 公历日期中文表示法 如一九九七年七月一日
/// </summary>
public string DateString
{
get
{
return "公元" + this._date.ToLongDateString();
}
}
#endregion #region IsLeapYear
/// <summary>
/// 当前是否公历闰年
/// </summary>
public bool IsLeapYear
{
get
{
return DateTime.IsLeapYear(this._date.Year);
}
}
#endregion #region ChineseConstellation
/// <summary>
/// 28星宿计算
/// </summary>
public string ChineseConstellation
{
get
{
int offset = ;
int modStarDay = ; TimeSpan ts = this._date - ChineseConstellationReferDay;
offset = ts.Days;
modStarDay = offset % ;
return (modStarDay >= ? _chineseConstellationName[modStarDay] : _chineseConstellationName[ + modStarDay]);
}
}
#endregion #region ChineseHour
/// <summary>
/// 时辰
/// </summary>
public string ChineseHour
{
get
{
return GetChineseHour(_datetime);
}
}
#endregion #endregion #region 农历日期
#region IsChineseLeapMonth
/// <summary>
/// 是否闰月
/// </summary>
public bool IsChineseLeapMonth
{
get { return this._cIsLeapMonth; }
}
#endregion #region IsChineseLeapYear
/// <summary>
/// 当年是否有闰月
/// </summary>
public bool IsChineseLeapYear
{
get
{
return this._cIsLeapYear;
}
}
#endregion #region ChineseDay
/// <summary>
/// 农历日
/// </summary>
public int ChineseDay
{
get { return this._cDay; }
}
#endregion #region ChineseDayString
/// <summary>
/// 农历日中文表示
/// </summary>
public string ChineseDayString
{
get
{
switch (this._cDay)
{
case :
return "";
case :
return "初十";
case :
return "二十";
case :
return "三十";
default:
return nStr2[(int)(_cDay / )].ToString() + nStr1[_cDay % ].ToString(); }
}
}
#endregion #region ChineseMonth
/// <summary>
/// 农历的月份
/// </summary>
public int ChineseMonth
{
get { return this._cMonth; }
}
#endregion #region ChineseMonthString
/// <summary>
/// 农历月份字符串
/// </summary>
public string ChineseMonthString
{
get
{
return _monthString[this._cMonth];
}
}
#endregion #region ChineseYear
/// <summary>
/// 取农历年份
/// </summary>
public int ChineseYear
{
get { return this._cYear; }
}
#endregion #region ChineseYearString
/// <summary>
/// 取农历年字符串如,一九九七年
/// </summary>
public string ChineseYearString
{
get
{
string tempStr = "";
string num = this._cYear.ToString();
for (int i = ; i < ; i++)
{
tempStr += ConvertNumToChineseNum(num[i]);
}
return tempStr + "年";
}
}
#endregion #region ChineseDateString
/// <summary>
/// 取农历日期表示法:农历一九九七年正月初五
/// </summary>
public string ChineseDateString
{
get
{
if (this._cIsLeapMonth == true)
{
return "农历" + ChineseYearString + "闰" + ChineseMonthString + ChineseDayString;
}
else
{
return "农历" + ChineseYearString + ChineseMonthString + ChineseDayString;
}
}
}
#endregion #region ChineseTwentyFourDay
/// <summary>
/// 定气法计算二十四节气,二十四节气是按地球公转来计算的,并非是阴历计算的
/// </summary>
/// <remarks>
/// 节气的定法有两种。古代历法采用的称为"恒气",即按时间把一年等分为24份,
/// 每一节气平均得15天有余,所以又称"平气"。现代农历采用的称为"定气",即
/// 按地球在轨道上的位置为标准,一周360°,两节气之间相隔15°。由于冬至时地
/// 球位于近日点附近,运动速度较快,因而太阳在黄道上移动15°的时间不到15天。
/// 夏至前后的情况正好相反,太阳在黄道上移动较慢,一个节气达16天之多。采用
/// 定气时可以保证春、秋两分必然在昼夜平分的那两天。
/// </remarks>
public string ChineseTwentyFourDay
{
get
{
DateTime baseDateAndTime = new DateTime(, , , , , ); //#1/6/1900 2:05:00 AM#
DateTime newDate;
double num;
int y;
string tempStr = ""; y = this._date.Year; for (int i = ; i <= ; i++)
{
num = 525948.76 * (y - ) + sTermInfo[i - ]; newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
if (newDate.DayOfYear == _date.DayOfYear)
{
tempStr = SolarTerm[i - ];
break;
}
}
return tempStr;
}
} //当前日期前一个最近节气
public string ChineseTwentyFourPrevDay
{
get
{
DateTime baseDateAndTime = new DateTime(, , , , , ); //#1/6/1900 2:05:00 AM#
DateTime newDate;
double num;
int y;
string tempStr = ""; y = this._date.Year; for (int i = ; i >= ; i--)
{
num = 525948.76 * (y - ) + sTermInfo[i - ]; newDate = baseDateAndTime.AddMinutes(num);//按分钟计算 if (newDate.DayOfYear < _date.DayOfYear)
{
tempStr = string.Format("{0}[{1}]", SolarTerm[i - ], newDate.ToString("yyyy-MM-dd"));
break;
}
} return tempStr;
} } //当前日期后一个最近节气
public string ChineseTwentyFourNextDay
{
get
{
DateTime baseDateAndTime = new DateTime(, , , , , ); //#1/6/1900 2:05:00 AM#
DateTime newDate;
double num;
int y;
string tempStr = ""; y = this._date.Year; for (int i = ; i <= ; i++)
{
num = 525948.76 * (y - ) + sTermInfo[i - ]; newDate = baseDateAndTime.AddMinutes(num);//按分钟计算 if (newDate.DayOfYear > _date.DayOfYear)
{
tempStr = string.Format("{0}[{1}]", SolarTerm[i - ], newDate.ToString("yyyy-MM-dd"));
break;
}
}
return tempStr;
} }
#endregion
#endregion #region 星座
#region Constellation
/// <summary>
/// 计算指定日期的星座序号
/// </summary>
/// <returns></returns>
public string Constellation
{
get
{
int index = ;
int y, m, d;
y = _date.Year;
m = _date.Month;
d = _date.Day;
y = m * + d; if (((y >= ) && (y <= ))) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) || (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else if ((y >= ) && (y <= )) { index = ; }
else { index = ; } return _constellationName[index];
}
}
#endregion
#endregion #region 属相
#region Animal
/// <summary>
/// 计算属相的索引,注意虽然属相是以农历年来区别的,但是目前在实际使用中是按公历来计算的
/// 鼠年为1,其它类推
/// </summary>
public int Animal
{
get
{
int offset = _date.Year - AnimalStartYear;
return (offset % ) + ;
}
}
#endregion #region AnimalString
/// <summary>
/// 取属相字符串
/// </summary>
public string AnimalString
{
get
{
int offset = _date.Year - AnimalStartYear; //阳历计算
//int offset = this._cYear - AnimalStartYear; 农历计算
return animalStr[offset % ].ToString();
}
}
#endregion
#endregion #region 天干地支
#region GanZhiYearString
/// <summary>
/// 取农历年的干支表示法如 乙丑年
/// </summary>
public string GanZhiYearString
{
get
{
string tempStr;
int i = (this._cYear - GanZhiStartYear) % ; //计算干支
tempStr = ganStr[i % ].ToString() + zhiStr[i % ].ToString() + "年";
return tempStr;
}
}
#endregion #region GanZhiMonthString
/// <summary>
/// 取干支的月表示字符串,注意农历的闰月不记干支
/// </summary>
public string GanZhiMonthString
{
get
{
//每个月的地支总是固定的,而且总是从寅月开始
int zhiIndex;
string zhi;
if (this._cMonth > )
{
zhiIndex = this._cMonth - ;
}
else
{
zhiIndex = this._cMonth + ;
}
zhi = zhiStr[zhiIndex - ].ToString(); //根据当年的干支年的干来计算月干的第一个
int ganIndex = ;
string gan;
int i = (this._cYear - GanZhiStartYear) % ; //计算干支
switch (i % )
{
#region ...
case : //甲
ganIndex = ;
break;
case : //乙
ganIndex = ;
break;
case : //丙
ganIndex = ;
break;
case : //丁
ganIndex = ;
break;
case : //戊
ganIndex = ;
break;
case : //己
ganIndex = ;
break;
case : //庚
ganIndex = ;
break;
case : //辛
ganIndex = ;
break;
case : //壬
ganIndex = ;
break;
case : //癸
ganIndex = ;
break;
#endregion
}
gan = ganStr[(ganIndex + this._cMonth - ) % ].ToString(); return gan + zhi + "月";
}
}
#endregion #region GanZhiDayString
/// <summary>
/// 取干支日表示法
/// </summary>
public string GanZhiDayString
{
get
{
int i, offset;
TimeSpan ts = this._date - GanZhiStartDay;
offset = ts.Days;
i = offset % ;
return ganStr[i % ].ToString() + zhiStr[i % ].ToString() + "日";
}
}
#endregion #region GanZhiDateString
/// <summary>
/// 取当前日期的干支表示法如 甲子年乙丑月丙庚日
/// </summary>
public string GanZhiDateString
{
get
{
return GanZhiYearString + GanZhiMonthString + GanZhiDayString;
}
}
#endregion
#endregion
#endregion #region 方法
#region NextDay
/// <summary>
/// 取下一天
/// </summary>
/// <returns></returns>
public ChineseCalendar NextDay()
{
DateTime nextDay = _date.AddDays();
return new ChineseCalendar(nextDay);
}
#endregion #region PervDay
/// <summary>
/// 取前一天
/// </summary>
/// <returns></returns>
public ChineseCalendar PervDay()
{
DateTime pervDay = _date.AddDays(-);
return new ChineseCalendar(pervDay);
}
#endregion
#endregion
}
}
调用:
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日
属相:蛇
农历:农历二零一二年腊月十六
时辰:庚申
节气:
节日:
前一个节气:大寒[--]
后一个节气:立春[--]
干支:壬辰年癸丑月癸巳日
星期:星期日
星宿:房日兔
星座:水瓶座
C#实现万年历(农历、节气、节日、星座、属相、生肖、闰年等)的更多相关文章
-
C#实现万年历(农历、节气、节日、星座、星宿、属相、生肖、闰年月、时辰)
C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰地址:http://www.cnblogs.com/txw1958/archive/2013/01/27/csharp-calend ...
-
php获取农历、节日、节气
/* * 农历 节气 节日 * edit: www.jbxue.com */ header("Content-Type:text/html;charset=utf-8"); cla ...
-
FullCalendar应用——整合农历节气和节日
FullCalendar用来做日程管理功能非常强大,但是唯一不足的地方是没有将中国农历历法加进去,今天我将结合实例和大家分享如何将中国农历中的节气和节日整合到FullCalendar中,从而增强其实用 ...
-
Google日历添加农历、节日和天气插件(步骤)
Google日历添加农历.节日和天气插件(步骤) Google功能非常多,Google日历只是其中一个,而且支持Exchange账户(iPhone,WP7,诺基亚等)和Google账户登录(andro ...
-
php 身份证号码获取星座和生肖
发布:thatboy 来源:Net [大 中 小] 本文介绍下,php用身份证号码获取星座和生肖的方法,一个简单的php实例,从身份证号码中取得星座与生肖信息,有兴趣的朋友参考研究下吧.本 ...
-
c#实现万年历示例分享 万年历农历查询
cs.cs(类页面) using System;using System.Collections.Generic;using System.Linq;using System.Web; namespa ...
-
PHP根据身份证号码验证、获取星座、生肖和性别函数
首先介绍一下身份证含义 新的18位身份证号码各位的含义:1-2位省.自治区.直辖市代码:3-4位地级市.盟.自治州代码:5-6位县.县级市.区代码:7-14位出生年月日,比如19670401代表196 ...
-
推荐一款万年历App 诸葛万年历
推荐一款万年历App 诸葛万年历 1 介绍 应用简介: 提供标准和专业的时间信息查询,记录和承载生活中的美好记忆,帮助用户高效快捷的管理个人时间.精美的日期展示和完善的重要事件提醒功能,可以方便安排日 ...
-
公历转农历的python实现
大杂烩.作为自己的记录,保存. 两个要点: 1.公历转农历用了查表法(第126行) 2.节气用了天文法?(第176行) 运行图 (背景是hao123万年历) 源代码: # lunar.py # 20 ...
随机推荐
-
在Oracle中恢复被DROP掉的表
在Oracle中可能不小心会DROP掉一个表,如果没有定期做备份的话,将会带来很大的麻烦.如果有的情况下,每天的数据都很重要,而定期备份的周期又稍长,情况恐怕也不容乐观!以前只知道Windows有个回 ...
-
【转】Android 底层开发的几点
我干了3年Android sdk开发,觉得到了瓶劲没法更进一步,于是花了一年多点时间,大概摸到点门径.根据前辈的经验,Android底层完全入门需要两年. 先说下我的入门过程:第零步,下载源码,我下的 ...
-
iPhone手机屏幕的尺寸
以下是 iPhone的型号和对应的屏幕宽高 英寸 宽 高 厚度 3.5 320 480 4s ipad 系列 4 320 568 5 5s 4.7 375 66 ...
-
extremeComponents(ec)源码分析
eXtremeComponents(简称ec)是一系列提供高级显示的开源JSP定制标签,当前的包含的组件为eXtremeTable,用于以表形式显示数据. 其本质是jsp的自定义标签,抓住这一点就抓住 ...
-
demo_03HTML5中的动画效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
-
转:Android模拟器连接电脑网络
原文地址:http://www.it165.net/pro/html/201212/4444.html 第一步: 在命令行(就是开始——运行——输入cmd)模式下输入adb shell命令一般会报两种 ...
-
AndroidUI的组成部分ProgressBar
package com.gc.progressbar; /* * 1.ProgressBar组件也是一组重要的组件,ProgressBar本身代表了进度条组件, * 它还派生了两个经常使用的组件:Se ...
-
away 3d的一些问题
不能成功draw m3u8视频流问题: Texture2DBase.as return context.createRectangleTexture(_width, _height, Context3 ...
-
Gym100971B Gym100971C Gym100971F Gym100971G Gym100971K Gym100971L(都是好写的题。。。) IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13, 2016
昨天训练打的Gym,今天写题解. Gym100971B 这个题就是输出的时候有点小问题,其他的都很简单. 总之,emnnn,简单题. 代码: #include<iostream> #inc ...
-
基于 Webpack 4 和 React hooks 搭建项目
面对日新月异的前端,我表示快学不动了