C#轉換農歷的簡單方法
作者:佚名
本文介紹C#轉換農歷,改裝了一下如DateTime時間形式,實現了公歷農歷轉換的功能,但是只能算到1900~2100年之間的。
C#轉換農歷
原來還準備自己寫算法,并研究農歷規則。發現那太難和麻煩了,光是農歷的推算那就我等專門研究歷法的人一下搞懂的。后來發現.NET類庫也提供一些基礎的農歷類System.Globalization.ChineseLunisolarCalendar。改裝了一下如DateTime時間形式,實現了公歷農歷轉換的功能,但是只能算到1900~2100年之間的。基本夠日常使用了。C#轉換農歷代碼如下:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- namespaceSystem
- ...{
- /**////<summary>
- ///中國常用農歷日期時間類
- ///zj53hao@qq.comhttp://hi.csdn.net/zj53hao
- ///</summary>
- classChinaDateTime
- ...{
- privateintyear,month,dayOfMonth;
- privateboolisLeap;
- publicDateTimetime;
- /**////<summary>
- ///獲取當前日期的農歷年
- ///</summary>
- publicintYear
- ...{
- get...{returnyear;}
- }
- /**////<summary>
- ///獲取當前日期的農歷月份
- ///</summary>
- publicintMonth
- ...{
- get...{returnmonth;}
- }
- /**////<summary>
- ///獲取當前日期的農歷月中天數
- ///</summary>
- publicintDayOfMonth
- ...{
- get...{returndayOfMonth;}
- }
- /**////<summary>
- ///獲取當前日期是否為閏月中的日期
- ///</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("參數日期時間不在支持的范圍內,支持范圍:
"+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#轉換農歷
【編輯推薦】
責任編輯:佚名
來源:
IT168