本文實例講述了C#實現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類。分享給大家供大家參考,具體如下:
最近郁悶地發(fā)現(xiàn)網(wǎng)上現(xiàn)有的相當(dāng)一部分萬年歷上干支紀(jì)年的算法都是錯誤的。因為干支紀(jì)年是針對陰歷而言的,而生肖屬相又跟地支對應(yīng),所以元旦和春節(jié)之間那段時間在干支紀(jì)年法中應(yīng)該歸上一年,以陽歷2007年2月9日為例,當(dāng)日的陰歷日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是瀏覽一下目前的萬年歷,相當(dāng)一部分都顯示成了丁亥年,豬年,比較郁悶~~
然后就寫了一個陰歷陽歷互相轉(zhuǎn)化的類。
相關(guān)代碼如下:
/// <summary>
/// 中國日歷信息實體類
/// </summary>
public sealed class ChineseCalendarInfo
{
private DateTime m_SolarDate;
private int m_LunarYear, m_LunarMonth, m_LunarDay;
private bool m_IsLeapMonth = false;
private string m_LunarYearSexagenary = null, m_LunarYearAnimal = null;
private string m_LunarYearText = null, m_LunarMonthText = null, m_LunarDayText = null;
private string m_SolarWeekText = null, m_SolarConstellation = null, m_SolarBirthStone = null;
日歷屬性
/// <summary>
/// 根據(jù)指定陽歷日期計算星座&誕生石
/// </summary>
/// <param name="date">指定陽歷日期</param>
/// <param name="constellation">星座</param>
/// <param name="birthstone">誕生石</param>
public static void CalcConstellation(DateTime date, out string constellation, out string birthstone)
{
int i = Convert.ToInt32(date.ToString("MMdd"));
int j;
if (i >= 321 && i <= 419)
j = 0;
else if (i >= 420 && i <= 520)
j = 1;
else if (i >= 521 && i <= 621)
j = 2;
else if (i >= 622 && i <= 722)
j = 3;
else if (i >= 723 && i <= 822)
j = 4;
else if (i >= 823 && i <= 922)
j = 5;
else if (i >= 923 && i <= 1023)
j = 6;
else if (i >= 1024 && i <= 1121)
j = 7;
else if (i >= 1122 && i <= 1221)
j = 8;
else if (i >= 1222 || i <= 119)
j = 9;
else if (i >= 120 && i <= 218)
j = 10;
else if (i >= 219 && i <= 320)
j = 11;
else
{
constellation = "未知星座";
birthstone = "未知誕生石";
return;
}
constellation = Constellations[j];
birthstone = BirthStones[j];
星座劃分
}
陰歷轉(zhuǎn)陽歷
從陰歷創(chuàng)建日歷
private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
public const string ChineseNumber = "〇一二三四五六七八九";
public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
public const string Animals = "鼠牛虎兔龍蛇馬羊猴雞狗豬";
public static readonly string[] ChineseWeekName = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public static readonly string[] ChineseDayName = new string[] {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
public static readonly string[] Constellations = new string[] { "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "雙魚座" };
public static readonly string[] BirthStones = new string[] { "鉆石", "藍(lán)寶石", "瑪瑙", "珍珠", "紅寶石", "紅條紋瑪瑙", "藍(lán)寶石", "貓眼石", "黃寶石", "土耳其玉", "紫水晶", "月長石,血石" };
}
附:完整實例代碼點擊此處本站下載。
PS:這里再為大家推薦幾款日歷相關(guān)在線工具供大家參考:
網(wǎng)頁萬年歷日歷:
http://tools.html5code.net/bianmin/webwannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.html5code.net/bianmin/yinli2yangli
在線萬年歷日歷:
http://tools.html5code.net/bianmin/wannianli
在線萬年歷黃歷flash版:
http://tools.html5code.net/bianmin/flashwnl
另外,本站歷史上的今天也有相似的農(nóng)歷日期顯示功能:
http://tools.html5code.net/bianmin/lishi
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#日期與時間操作技巧總結(jié)》、《C#字符串操作技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。