本文實例講述了C#實現的陰歷陽歷互相轉化類。分享給大家供大家參考,具體如下:
最近郁悶地發現網上現有的相當一部分萬年歷上干支紀年的算法都是錯誤的。因為干支紀年是針對陰歷而言的,而生肖屬相又跟地支對應,所以元旦和春節之間那段時間在干支紀年法中應該歸上一年,以陽歷2007年2月9日為例,當日的陰歷日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是瀏覽一下目前的萬年歷,相當一部分都顯示成了丁亥年,豬年,比較郁悶~~
然后就寫了一個陰歷陽歷互相轉化的類。
相關代碼如下:
/// <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>
/// 根據指定陽歷日期計算星座&誕生石
/// </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];
星座劃分
}
陰歷轉陽歷
從陰歷創建日歷
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[] { "鉆石", "藍寶石", "瑪瑙", "珍珠", "紅寶石", "紅條紋瑪瑙", "藍寶石", "貓眼石", "黃寶石", "土耳其玉", "紫水晶", "月長石,血石" };
}
附:完整實例代碼點擊此處本站下載。
PS:這里再為大家推薦幾款日歷相關在線工具供大家參考:
網頁萬年歷日歷:
http://tools.html5code.net/bianmin/webwannianli
在線陰歷/陽歷轉換工具:
http://tools.html5code.net/bianmin/yinli2yangli
在線萬年歷日歷:
http://tools.html5code.net/bianmin/wannianli
在線萬年歷黃歷flash版:
http://tools.html5code.net/bianmin/flashwnl
另外,本站歷史上的今天也有相似的農歷日期顯示功能:
http://tools.html5code.net/bianmin/lishi
更多關于C#相關內容感興趣的讀者可查看本站專題:《C#日期與時間操作技巧總結》、《C#字符串操作技巧總結》、《C#數組操作技巧總結》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》及《C#面向對象程序設計入門教程》
希望本文所述對大家C#程序設計有所幫助。
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!