ASP.NET顯示農(nóng)歷時(shí)間改進(jìn)版
本文實(shí)例講述了ASP.NET顯示農(nóng)歷時(shí)間的方法,是前面一篇文章源碼的改進(jìn)版。分享給大家供大家參考。具體如下:
前面有一篇取農(nóng)歷時(shí)間http://www.dhdzp.com/article/57481.htm,不過沒有進(jìn)行封裝使用起來需要手動(dòng)修改。本次進(jìn)行簡單封裝一下,可以直接進(jìn)行調(diào)用。
代碼如下:
取農(nóng)歷時(shí)間的類
{
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>
/// 獲取一個(gè)公歷日期對(duì)應(yīng)的完整的農(nóng)歷日期
/// </summary>
/// <param name="time">一個(gè)公歷日期</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>
/// 獲取一個(gè)公歷日期的農(nóng)歷干支紀(jì)年
/// </summary>
/// <param name="time">一個(gè)公歷日期</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>
/// 獲取一個(gè)公歷日期的農(nóng)歷年份
/// </summary>
/// <param name="time">一個(gè)公歷日期</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>
/// 獲取一個(gè)公歷日期的農(nóng)歷月份
/// </summary>
/// <param name="time">一個(gè)公歷日期</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; //一年中最多有一個(gè)閏月
}
}
if (leap > 0) month--;
return (leap == month + 1 ? "閏" : "") + ChineseMonthName[month - 1];
}
/// <summary>
/// 獲取一個(gè)公歷日期的農(nóng)歷日
/// </summary>
/// <param name="time">一個(gè)公歷日期</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();//公歷日期
下面有一個(gè)測試的效果:
前臺(tái)代碼:
<!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)歷時(shí)間"/></td>
<td><asp:Label ID="lblCountryDate" runat="server"/></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="公歷時(shí)間"/></td>
<td><asp:Label ID="lblForignDate" runat="server"/></td>
</tr>
</table>
<asp:Button ID="buttton1" runat="server" Text="顯示時(shí)間" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
后臺(tái)代碼:
{
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)行效果如下圖所示:

主要取時(shí)間就是這個(gè)CountryDate類,調(diào)用取時(shí)間即可。
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
相關(guān)文章
ASP.NET中控件的EnableViewState屬性及徹底禁用
如果我們?cè)陂_發(fā)Web應(yīng)用程序時(shí),某些控件是不需要接受用戶的操作或只需要接受一次操作的時(shí)候,我們可以將這些控件的EnableViewState屬性改為false,這樣可以優(yōu)化我們的程序,提高網(wǎng)絡(luò)訪問的速度。2016-06-06
ASP.NET實(shí)現(xiàn)可以縮放和旋轉(zhuǎn)的圖片預(yù)覽頁效果
本文詳細(xì)介紹了如何在ASP.NET?WebForms中實(shí)現(xiàn)一個(gè)功能豐富的圖片預(yù)覽頁面,通過結(jié)合HTML、CSS和JavaScript,用戶可以方便地對(duì)圖片進(jìn)行放大、縮小以及旋轉(zhuǎn)操作,感興趣的朋友跟隨小編一起看看吧2024-08-08
WPF項(xiàng)目在設(shè)計(jì)界面調(diào)用后臺(tái)代碼
這篇文章介紹了WPF項(xiàng)目在設(shè)計(jì)界面調(diào)用后臺(tái)代碼的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
ASP.NET Core擴(kuò)展庫之實(shí)體映射使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之實(shí)體映射使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-03-03
ASP.Net MVC+Data Table實(shí)現(xiàn)分頁+排序功能的方法
這篇文章主要介紹了ASP.Net MVC+Data Table實(shí)現(xiàn)分頁+排序功能的方法,結(jié)合實(shí)例形式分析了asp.net基于mvc架構(gòu)實(shí)現(xiàn)的數(shù)據(jù)查詢、排序、分頁顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
asp.net實(shí)現(xiàn)的群發(fā)郵件功能詳解
這篇文章主要介紹了asp.net實(shí)現(xiàn)的群發(fā)郵件功能,結(jié)合具體實(shí)例形式分析了asp.net基于SMTP服務(wù)群發(fā)QQ郵件的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-05-05
運(yùn)用.net core中實(shí)例講解RabbitMQ高可用集群構(gòu)建
這篇文章主要介紹了運(yùn)用.net core中實(shí)例講解RabbitMQ高可用集群構(gòu)建,文中相關(guān)示例代碼講解的非常清晰,感興趣的小伙伴可以參考一下這篇文章,相信可以幫助到你2021-09-09
實(shí)現(xiàn).Net7下數(shù)據(jù)庫定時(shí)檢查的方法詳解
在軟件開發(fā)過程中,有時(shí)候我們需要定時(shí)地檢查數(shù)據(jù)庫中的數(shù)據(jù),并在發(fā)現(xiàn)新增數(shù)據(jù)時(shí)觸發(fā)一個(gè)動(dòng)作。為了實(shí)現(xiàn)這個(gè)需求,本文我們?cè)?.Net?7?下進(jìn)行一次簡單的演示。感興趣的可以了解一下2022-12-12

