Dot Net 平台,對全球化的支援做的非常好,不得不稱讚一個
通常,將西曆轉為農曆,是個非常煩的事情,需要整理閏年、閏月等的對照表。
在.Net平台上,有了國際化的支援,這些東西,都已經提供了 ,我們需要做的,只是利用一下而已。
話不多說,直接上代碼:
/// <summary>/// 西曆轉為農曆的函數/// </summary>/// <remarks>作者:三角貓 DeltaCat</remarks>/// <example>網址:http://www.zu14.cn</example>/// <param name="solarDateTime">西曆日期</param>/// <returns>農曆的日期</returns>static string SolarToChineseLunisolarDate(DateTime solarDateTime){ System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar(); int year = cal.GetYear(solarDateTime); int month = cal.GetMonth(solarDateTime); int day = cal.GetDayOfMonth(solarDateTime); int leapMonth = cal.GetLeapMonth(year); return string.Format("農曆{0}{1}({2})年{3}{4}月{5}{6}" , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10] , "子醜寅卯辰巳午未申酉戌亥"[(year - 4) % 12] , "鼠牛虎兔龍蛇馬羊猴雞狗豬"[(year - 4) % 12] , month == leapMonth ? "閏" : "" , "無正二三四五六七八九十冬臘"[leapMonth > 0 && leapMonth <= month ? month - 1 : month] , "初十廿三"[day / 10] , "日一二三四五六七八九"[day % 10] );}
使用的方法非常簡單:
string 農曆 = SolarToChineseLunisolarDate(DateTime.Today);
[ 閱讀全文 ]