ASP.NET显示农历时间的方法
本文实例讲述了ASP.NET显示农历时间的方法。分享给大家供大家参考。具体实现方法如下:
CS部分代码如下:
publicstringChineseTimeNow=""; publicstringForignTimeNow=""; privatestaticChineseLunisolarCalendarcalendar=newChineseLunisolarCalendar(); privatestaticstringChineseNumber="〇一二三四五六七八九"; publicconststringCelestialStem="甲乙丙丁戊己庚辛壬癸"; publicconststringTerrestrialBranch="子丑寅卯辰巳午未申酉戌亥"; publicstaticreadonlystring[]ChineseDayName=newstring[]{ "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}; publicstaticreadonlystring[]ChineseMonthName=newstring[]{"正","二","三","四","五","六","七","八","九","十","十一","十二"};
overrideprotectedvoidPage_Load(objectsender,EventArgse) { base.Page_Load(sender,e); userName=Session["Admin"].ToString(); ChineseTimeNow=GetChineseDate(DateTime.Now); ForignTimeNow=DateTime.Now.GetDateTimeFormats('D')[0].ToString(); }
///<summary> ///获取一个公历日期对应的完整的农历日期 ///</summary> ///<paramname="time">一个公历日期</param> ///<returns>农历日期</returns> publicstringGetChineseDate(DateTimetime) { stringstrY=GetYear(time); stringstrM=GetMonth(time); stringstrD=GetDay(time); stringstrSB=GetStemBranch(time); stringstrDate=strY+"("+strSB+")年"+strM+"月"+strD; returnstrDate; } ///<summary> ///获取一个公历日期的农历干支纪年 ///</summary> ///<paramname="time">一个公历日期</param> ///<returns>农历干支纪年</returns> publicstringGetStemBranch(DateTimetime) { intsexagenaryYear=calendar.GetSexagenaryYear(time); stringstemBranch=CelestialStem.Substring(sexagenaryYear%10-1,1)+TerrestrialBranch.Substring(sexagenaryYear%12-1,1); returnstemBranch; }
///<summary> ///获取一个公历日期的农历年份 ///</summary> ///<paramname="time">一个公历日期</param> ///<returns>农历年份</returns> publicstringGetYear(DateTimetime) { StringBuildersb=newStringBuilder(); intyear=calendar.GetYear(time); intd; do { d=year%10; sb.Insert(0,ChineseNumber[d]); year=year/10; }while(year>0); returnsb.ToString(); }
///<summary> ///获取一个公历日期的农历月份 ///</summary> ///<paramname="time">一个公历日期</param> ///<returns>农历月份</returns> publicstringGetMonth(DateTimetime) { intmonth=calendar.GetMonth(time); intyear=calendar.GetYear(time); intleap=0; //正月不可能闰月 for(inti=3;i<=month;i++) { if(calendar.IsLeapMonth(year,i)) { leap=i; break;//一年中最多有一个闰月 } } if(leap>0)month--; return(leap==month+1?"闰":"")+ChineseMonthName[month-1]; }
///<summary> ///获取一个公历日期的农历日 ///</summary> ///<paramname="time">一个公历日期</param> ///<returns>农历日</returns> publicstringGetDay(DateTimetime) { returnChineseDayName[calendar.GetDayOfMonth(time)-1]; }