本篇內(nèi)容介紹了“ASP.NET顯示農(nóng)歷時(shí)間的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)建站長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鎮(zhèn)遠(yuǎn)企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),鎮(zhèn)遠(yuǎn)網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。取農(nóng)歷時(shí)間的類
代碼如下:
public class CountryDate
{
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
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Globalization;
調(diào)用:
復(fù)制代碼 代碼如下:
CountryDate cd = new CountryDate();
string ChineseTimeNow = cd.GetChineseDate(DateTime.Now);//農(nóng)歷日期
string ForignTimeNow = DateTime.Now.GetDateTimeFormats('D')[0].ToString();//公歷日期
下面有一個(gè)測(cè)試的效果:
前臺(tái)代碼:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestCountryDate._Default" %>
<!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)代碼:
復(fù)制代碼 代碼如下:
public partial class _Default : System.Web.UI.Page
{
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í)間即可。
“ASP.NET顯示農(nóng)歷時(shí)間的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
文章題目:ASP.NET顯示農(nóng)歷時(shí)間的方法-創(chuàng)新互聯(lián)
本文來源:http://muchs.cn/article42/ceosec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、搜索引擎優(yōu)化、商城網(wǎng)站、微信小程序、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容