本篇內容主要講解“ASP.NET如何顯示農歷時間”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ASP.NET如何顯示農歷時間”吧!
10年建站經驗, 成都網站制作、網站建設、外貿網站建設客戶的見證與正確選擇。成都創(chuàng)新互聯提供完善的營銷型網頁建站明細報價表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。本文實例講述了ASP.NET顯示農歷時間的方法。分享給大家供大家參考。具體實現方法如下:
CS部分代碼如下:
復制代碼 代碼如下:
public string ChineseTimeNow = "";
public string ForignTimeNow = "";
private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
private static string ChineseNumber = "〇一二三四五六七八九";
public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
public static readonly string[] ChineseDayName = new string[] {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
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();
}
///
/// 獲取一個公歷日期對應的完整的農歷日期
///
/// 一個公歷日期
///
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;
}
///
/// 獲取一個公歷日期的農歷干支紀年
///
/// 一個公歷日期
///
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;
}
///
/// 獲取一個公歷日期的農歷年份
///
/// 一個公歷日期
///
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();
}
///
/// 獲取一個公歷日期的農歷月份
///
/// 一個公歷日期
///
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];
}
///
/// 獲取一個公歷日期的農歷日
///
/// 一個公歷日期
///
public string GetDay(DateTime time)
{
return ChineseDayName[calendar.GetDayOfMonth(time) - 1];
}
前臺部分代碼如下:
復制代碼 代碼如下:
<%=ForignTimeNow%> | >
<%=ChineseTimeNow %> |
到此,相信大家對“ASP.NET如何顯示農歷時間”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯建站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!