时间:2020-10-25 09:57:24 | 栏目:.NET代码 | 点击:次
本文实例讲述了ASP.NET显示农历时间的方法。分享给大家供大家参考。具体实现方法如下:
CS部分代码如下:
override protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
userName = Session["Admin"].ToString();
ChineseTimeNow=GetChineseDate(DateTime.Now);
ForignTimeNow = DateTime.Now.GetDateTimeFormats('D')[0].ToString();
}
/// <summary>
/// 获取一个公历日期对应的完整的农历日期
/// </summary>
/// <param name="time">一个公历日期</param>
/// <returns>农历日期</returns>
public string GetChineseDate(DateTime time)
{
string strY = GetYear(time);
string strM = GetMonth(time);
string strD = GetDay(time);
string strSB = GetStemBranch(time);
string strDate = strY + "(" + strSB + ")年 " + strM + "月 " + strD;
return strDate;
}
/// <summary>
/// 获取一个公历日期的农历干支纪年
/// </summary>
/// <param name="time">一个公历日期</param>
/// <returns>农历干支纪年</returns>
public string GetStemBranch(DateTime time)
{
int sexagenaryYear = calendar.GetSexagenaryYear(time);
string stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 - 1, 1);
return stemBranch;
}
/// <summary>
/// 获取一个公历日期的农历年份
/// </summary>
/// <param name="time">一个公历日期</param>
/// <returns>农历年份</returns>
public string GetYear(DateTime time)
{
StringBuilder sb = new StringBuilder();
int year = calendar.GetYear(time);
int d;
do
{
d = year % 10;
sb.Insert(0, ChineseNumber[d]);
year = year / 10;
} while (year > 0);
return sb.ToString();
}
/// <summary>
/// 获取一个公历日期的农历月份
/// </summary>
/// <param name="time">一个公历日期</param>
/// <returns>农历月份</returns>
public string GetMonth(DateTime time)
{
int month = calendar.GetMonth(time);
int year = calendar.GetYear(time);
int leap = 0;
//正月不可能闰月
for (int i = 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>
/// <param name="time">一个公历日期</param>
/// <returns>农历日</returns>
public string GetDay(DateTime time)
{
return ChineseDayName[calendar.GetDayOfMonth(time) - 1];
}
前台部分代码如下:
希望本文所述对大家的ASP.NET程序设计有所帮助。