C#轉(zhuǎn)換農(nóng)歷的簡單方法
C#轉(zhuǎn)換農(nóng)歷
原來還準(zhǔn)備自己寫算法,并研究農(nóng)歷規(guī)則。發(fā)現(xiàn)那太難和麻煩了,光是農(nóng)歷的推算那就我等專門研究歷法的人一下搞懂的。后來發(fā)現(xiàn).NET類庫也提供一些基礎(chǔ)的農(nóng)歷類System.Globalization.ChineseLunisolarCalendar。改裝了一下如DateTime時間形式,實現(xiàn)了公歷農(nóng)歷轉(zhuǎn)換的功能,但是只能算到1900~2100年之間的?;緣蛉粘J褂昧恕#轉(zhuǎn)換農(nóng)歷代碼如下:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- namespaceSystem
- ...{
- /**////<summary>
- ///中國常用農(nóng)歷日期時間類
- ///zj53hao@qq.comhttp://hi.csdn.net/zj53hao
- ///</summary>
- classChinaDateTime
- ...{
- privateintyear,month,dayOfMonth;
- privateboolisLeap;
- publicDateTimetime;
- /**////<summary>
- ///獲取當(dāng)前日期的農(nóng)歷年
- ///</summary>
- publicintYear
- ...{
- get...{returnyear;}
- }
- /**////<summary>
- ///獲取當(dāng)前日期的農(nóng)歷月份
- ///</summary>
- publicintMonth
- ...{
- get...{returnmonth;}
- }
- /**////<summary>
- ///獲取當(dāng)前日期的農(nóng)歷月中天數(shù)
- ///</summary>
- publicintDayOfMonth
- ...{
- get...{returndayOfMonth;}
- }
- /**////<summary>
- ///獲取當(dāng)前日期是否為閏月中的日期
- ///</summary>
- publicboolIsLeap
- ...{
- get...{returnisLeap;}
- }
- System.Globalization.ChineseLunisolarCalendarcc;
- /**////<summary>
- ///返回指定公歷日期的陰歷時間
- ///</summary>
- ///<paramnameparamname="time"></param>
- publicChinaDateTime(DateTimetime)
- ...{
- cc=newSystem.Globalization.ChineseLunisolarCalendar();
- if(time>cc.MaxSupportedDateTime||time<cc.MinSupportedDateTime)
- thrownewException("參數(shù)日期時間不在支持的范圍內(nèi),支持范圍:
"+cc.MinSupportedDateTime.ToShortDateString()+"到
"+cc.MaxSupportedDateTime.ToShortDateString());- year=cc.GetYear(time);
- month=cc.GetMonth(time);
- dayOfMonth=cc.GetDayOfMonth(time);
- isLeap=cc.IsLeapMonth(year,month);
- if(isLeap)month-=1;
- this.time=time;
- }
以上介紹C#轉(zhuǎn)換農(nóng)歷
【編輯推薦】