ASP.NET顯示農(nóng)歷時間改進(jìn)版
本文實(shí)例講述了ASP.NET顯示農(nóng)歷時間的方法,是前面一篇文章源碼的改進(jìn)版。分享給大家供大家參考。具體如下:
前面有一篇取農(nóng)歷時間http://www.dbjr.com.cn/article/57481.htm,不過沒有進(jìn)行封裝使用起來需要手動修改。本次進(jìn)行簡單封裝一下,可以直接進(jìn)行調(diào)用。
代碼如下:
取農(nóng)歷時間的類
{
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[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
/// <summary>
/// 獲取一個公歷日期對應(yīng)的完整的農(nóng)歷日期
/// </summary>
/// <param name="time">一個公歷日期</param>
/// <returns>農(nóng)歷日期</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>
/// 獲取一個公歷日期的農(nóng)歷干支紀(jì)年
/// </summary>
/// <param name="time">一個公歷日期</param>
/// <returns>農(nóng)歷干支紀(jì)年</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>
/// 獲取一個公歷日期的農(nóng)歷年份
/// </summary>
/// <param name="time">一個公歷日期</param>
/// <returns>農(nóng)歷年份</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>
/// 獲取一個公歷日期的農(nóng)歷月份
/// </summary>
/// <param name="time">一個公歷日期</param>
/// <returns>農(nóng)歷月份</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>
/// 獲取一個公歷日期的農(nóng)歷日
/// </summary>
/// <param name="time">一個公歷日期</param>
/// <returns>農(nóng)歷日</returns>
public string GetDay(DateTime time)
{
return ChineseDayName[calendar.GetDayOfMonth(time) - 1];
}
}
需要的using
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Globalization;
調(diào)用:
string ChineseTimeNow = cd.GetChineseDate(DateTime.Now);//農(nóng)歷日期
string ForignTimeNow = DateTime.Now.GetDateTimeFormats('D')[0].ToString();//公歷日期
下面有一個測試的效果:
前臺代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="農(nóng)歷時間"/></td>
<td><asp:Label ID="lblCountryDate" runat="server"/></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="公歷時間"/></td>
<td><asp:Label ID="lblForignDate" runat="server"/></td>
</tr>
</table>
<asp:Button ID="buttton1" runat="server" Text="顯示時間" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
后臺代碼:
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
CountryDate cd = new CountryDate();
string ChineseTimeNow = cd.GetChineseDate(DateTime.Now);//農(nóng)歷日期
string ForignTimeNow = DateTime.Now.GetDateTimeFormats('D')[0].ToString();//公歷日期
lblCountryDate.Text = ChineseTimeNow;
lblForignDate.Text = ForignTimeNow;
}
}
運(yùn)行效果如下圖所示:
主要取時間就是這個CountryDate類,調(diào)用取時間即可。
希望本文所述對大家的asp.net程序設(shè)計有所幫助。
相關(guān)文章
ASP.NET記住登陸用戶名的具體實(shí)現(xiàn)
ASP.NET記住登陸用戶名的具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06asp.net 簡易生成注冊碼(數(shù)字+大小寫字母)
注釋寫的很詳細(xì),不做過多的描述了,希望能給初學(xué)者帶來一些幫助,同時也是自己知識的一個積累過程。2008-11-11先裝了FRAMEWORK,后裝IIS導(dǎo)致asp.net頁面無法訪問的解決方法
如果先裝了FRAMEWORK,后裝IIS。有可能沒有在IIS中注冊,就會導(dǎo)致在頁面中無法訪問的情況2012-01-01ASP.NET Eval進(jìn)行數(shù)據(jù)綁定的方法
ASP.NET Eval在數(shù)據(jù)綁定方面的應(yīng)用是眾所周知的,不過技術(shù)在發(fā)展,當(dāng)ASP.NET Eval 1.1變成ASP.NET Eval 2.0的時候,在操作的時候會有什么變化呢?2013-04-04VB.NET拷貝整個目錄下所有子目錄及文件的實(shí)例代碼
這篇文章介紹了拷貝整個目錄下所有子目錄及文件的代碼,有需要的朋友可以參考一下2013-07-07