C#实现带阴历显示的日期代码
本文实例讲述了C#实现带阴历显示的日期代码,分享给大家供大家参考。具体方法如下:
这是一个用于酒店预定功能的带日期控件,类似去哪网酒店预定,由于需要设置节假日不同时期内的价格,因此需要自己写个时间控件。在此分享下写时间控件过程中用到的农历显示类。
publicclassCnCalendar { staticChineseLunisolarCalendarcCalendar=newChineseLunisolarCalendar(); publicstaticstringGetChineseDateTime(DateTimedatetime) { stringstrDate=datetime.Month+"月"+datetime.Day+"日"; intlyear=cCalendar.GetYear(datetime); intlmonth=cCalendar.GetMonth(datetime); intlday=cCalendar.GetDayOfMonth(datetime); //获取闰月,0则表示没有闰月 intleapMonth=cCalendar.GetLeapMonth(lyear); boolisleap=false; if(leapMonth>0) { if(leapMonth==lmonth) { //闰月 isleap=true; lmonth--; } elseif(lmonth>leapMonth) { lmonth--; } } if(strDate=="1月1日") { return"<emclass='calendarNLJieri'>元旦</em>"; } elseif(strDate=="2月14日") { return"<emclass='calendarNLJieri'>情人节</em>"; } elseif(strDate=="3月8日") { return"<emclass='calendarNLJieri'>妇女节</em>"; } elseif(strDate=="3月12日") { return"<emclass='calendarNLJieri'>植树节</em>"; } elseif(strDate=="4月1日") { return"<emclass='calendarNLJieri'>愚人节</em>"; } elseif(strDate=="5月1日") { return"<emclass='calendarNLJieri'>劳动节</em>"; } elseif(strDate=="5月4日") { return"<emclass='calendarNLJieri'>青年节</em>"; } elseif(strDate=="6月1日") { return"<emclass='calendarNLJieri'>儿童节</em>"; } elseif(strDate=="8月1日") { return"<emclass='calendarNLJieri'>建军节</em>"; } elseif(strDate=="9月10日") { return"<emclass='calendarNLJieri'>教师节</em>"; } elseif(strDate=="10月1日") { return"<emclass='calendarNLJieri'>国庆节</em>"; } else { if(lday==1) { return"<emclass='calendarNL'>"+ string.Concat(isleap?"闰":string.Empty,GetLunisolarMonth(lmonth),"月")+"</em>"; } else { stringstrNongli=string.Concat(isleap?"闰":string.Empty,GetLunisolarMonth(lmonth),"月", GetLunisolarDay(lday)); switch(strNongli) { case"正月初一": return"<emclass='calendarNLJieri'>春节</em>"; case"正月十五": return"<emclass='calendarNLJieri'>元宵节</em>"; case"五月初五": return"<emclass='calendarNLJieri'>端午节</em>"; case"七月初七": return"<emclass='calendarNLJieri'>七夕</em>"; case"八月十五": return"<emclass='calendarNLJieri'>中秋节</em>"; case"九月初九": return"<emclass='calendarNLJieri'>重阳节</em>"; case"腊月初八": return"<emclass='calendarNLJieri'>腊八节</em>"; case"腊月二十四": return"<emclass='calendarNLJieri'>扫房节</em>"; default: return"<emclass='calendarNL'>"+GetLunisolarDay(lday)+"</em>"; } } } }
#region农历年 ///<summary> ///十天干 ///</summary> privatestaticstring[]tiangan={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
///<summary> ///十二地支 ///</summary> privatestaticstring[]dizhi={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
///<summary> ///十二生肖 ///</summary> privatestaticstring[]shengxiao={"鼠","牛","虎","免","龙","蛇","马","羊","猴","鸡","狗","猪"};
///<summary> ///返回农历天干地支年 ///</summary> ///<paramname="year">农历年</param> ///<returns></returns> publicstaticstringGetLunisolarYear(intyear) { if(year>3) { inttgIndex=(year-4)%10; intdzIndex=(year-4)%12;
returnstring.Concat(tiangan[tgIndex],dizhi[dzIndex],"[",shengxiao[dzIndex],"]");
}
thrownewArgumentOutOfRangeException("无效的年份!"); } #endregion
#region农历月
///<summary> ///农历月 ///</summary> privatestaticstring[]months={"正","二","三","四","五","六","七","八","九","十","十一","腊月"};
///<summary> ///返回农历月 ///</summary> ///<paramname="month">月份</param> ///<returns></returns> publicstaticstringGetLunisolarMonth(intmonth) { if(month<13&&month>0) { returnmonths[month-1]; }
thrownewArgumentOutOfRangeException("无效的月份!"); }
#endregion
#region农历日
///<summary> /// ///</summary> privatestaticstring[]days1={"初","十","廿","三"};
///<summary> ///日 ///</summary> privatestaticstring[]days={"一","二","三","四","五","六","七","八","九","十"};
///<summary> ///返回农历日 ///</summary> ///<paramname="day"></param> ///<returns></returns> publicstaticstringGetLunisolarDay(intday) { if(day>0&&day<32) { if(day!=20&&day!=30) { returnstring.Concat(days1[(day-1)/10],days[(day-1)%10]); } else { returnstring.Concat(days[(day-1)/10],days1[1]); } }
thrownewArgumentOutOfRangeException("无效的日!"); }
#endregion
///<summary> ///返回生肖 ///</summary> ///<paramname="datetime">公历日期</param> ///<returns></returns> publicstaticstringGetShengXiao(DateTimedatetime) { returnshengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime))-1]; } }