C#公历转农历
2010-04-27 08:31
316 查看
在.net2.0的版本中的System.Globalization.ChineseLunisolarCalendar是针对中国的日历类,公历与中国传统农历纪年之间的相互转换,利用它可以计算天干地支等有关农历的信息。
使用
测试的结果:
传入日期:2010-3-4
返回农历:庚寅[虎]年正月十九
可以满足简单的需求啦。
其他代码也附上:
还有一个根据日期获取生肖的代码:
阅读全文
类别:Net查看评论
使用
staticChineseLunisolarCalendarcCalendar=newChineseLunisolarCalendar();
cCalendar.MaxSupportedDateTime返回支持的最大日期,即2101-1-28
cCalendar.MinSupportedDateTime返回支持的最小日期,即1901-2-19
下面我们来实现公历转农历。
///<summary>
///根据公历获取农历日期
///</summary>
///<paramname="datetime">公历日期</param>
///<returns></returns>
publicstaticstringGetChineseDateTime(DateTimedatetime)
{
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--;
}
}
returnstring.Concat(GetLunisolarYear(lyear),"年",isleap?"闰":string.Empty,GetLunisolarMonth(lmonth),"月",GetLunisolarDay(lday));
}
测试的结果:
传入日期:2010-3-4
返回农历:庚寅[虎]年正月十九
可以满足简单的需求啦。
其他代码也附上:
#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];
}
source:http://www.cnblogs.com/huxj/archive/2010/03/04/1678160.html
类别:
相关文章推荐
- 纯ASP代码之公历转农历实现(含属相)
- 公历,农历日期
- 很强大的Js日历代码,包括公历和农历的节日,不过界面设计一般
- java版公历转农历
- 阳历(公历)转农历 1801-2099
- 【转帖】试论公历与农历的比较
- 公历转农历的程序(代码转载于网络)
- JS:公历、农历互转
- 显示公历农历干支的Delphi控件: LssCalendar 2.0
- C# 查农历 阴历 阳历 公历 节假日
- 4000 C++:公历转农历的程序
- Python实现公历(阳历)转农历(阴历)的方法示例
- 公历转农历(C版本)
- 纯ASP代码之公历转农历实现(含属相) --例子代码
- 发布一个简陋的公历转换农历小工具
- 纯ASP代码之公历转农历实现(含属相)
- 公历转换为农历
- 在网页上显示农历公历的JavaScript代码
- 公历到农历的转换法
- 公历转农历(C版本)