C#(ASP.NET)西曆轉農曆的簡單方法

來源:互聯網
上載者:User

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);

[ 閱讀全文 ]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.