新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#轉(zhuǎn)換農(nóng)歷的簡單方法
C#轉(zhuǎn)換農(nóng)歷

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)云和免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
原來還準(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褂昧?。C#轉(zhuǎn)換農(nóng)歷代碼如下:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- namespaceSystem
- ...{
- /**////
- ///中國常用農(nóng)歷日期時間類
- ///zj53hao@qq.comhttp://hi.csdn.net/zj53hao
- ///
- classChinaDateTime
- ...{
- privateintyear,month,dayOfMonth;
- privateboolisLeap;
- publicDateTimetime;
- /**////
- ///獲取當(dāng)前日期的農(nóng)歷年
- ///
- publicintYear
- ...{
- get...{returnyear;}
- }
- /**////
- ///獲取當(dāng)前日期的農(nóng)歷月份
- ///
- publicintMonth
- ...{
- get...{returnmonth;}
- }
- /**////
- ///獲取當(dāng)前日期的農(nóng)歷月中天數(shù)
- ///
- publicintDayOfMonth
- ...{
- get...{returndayOfMonth;}
- }
- /**////
- ///獲取當(dāng)前日期是否為閏月中的日期
- ///
- publicboolIsLeap
- ...{
- get...{returnisLeap;}
- }
- System.Globalization.ChineseLunisolarCalendarcc;
- /**////
- ///返回指定公歷日期的陰歷時間
- ///
- ///<paramnameparamname="time">
- publicChinaDateTime(DateTimetime)
- ...{
- cc=newSystem.Globalization.ChineseLunisolarCalendar();
- if(time>cc.MaxSupportedDateTime||time
) - 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)歷
當(dāng)前文章:C#轉(zhuǎn)換農(nóng)歷的簡單方法
當(dāng)前URL:http://www.dlmjj.cn/article/coecpjc.html


咨詢
建站咨詢
