C# 阳历转农历
你妹的sb 原文 C#(ASP.NET)公歷轉(zhuǎn)農(nóng)歷的簡單方法
Dot Net 平臺,對全球化的支持做的非常好,不得不稱贊一個
通常,將公歷轉(zhuǎn)為農(nóng)歷,是個非常煩的事情,需要整理閏年、閏月等的對照表。
在.Net平臺上,有了國際化的支持,這些東西,都已經(jīng)提供了 ,我們需要做的,只是利用一下而已。
話不多說,直接上代碼:
?
/// <summary> /// 公歷轉(zhuǎn)為農(nóng)歷的函數(shù) /// </summary> /// <remarks>作者:DeltaCat</remarks> /// <example>網(wǎng)址:http://www.zu14.cn</example> /// <param name="solarDateTime">公歷日期</param> /// <returns>農(nóng)歷的日期</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("農(nóng)歷{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 農(nóng)歷 = SolarToChineseLunisolarDate(DateTime.Today);
?
總結(jié)
- 上一篇: docker(部署常见应用):docke
- 下一篇: nginx菜鸟教程二