ASP.NET显示农历时间
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET显示农历时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天看了下之前寫的網頁,突然想起應該加個時間的。
于是準備就直接DateTime.Now一下,再一想,還是顯示農歷吧,畢竟這是咱們中國人自己的時間,
使用: <table style=""><tr valign="bottom"> <td valign="bottom"> <%=ForignTimeNow%><br /></td>></tr><tr valign="bottom"><td valign="bottom"> <%=ChineseTimeNow %></td></tr></table>
url: http://greatverve.cnblogs.com/archive/2011/09/23/Lunar-calendar.html
于是準備就直接DateTime.Now一下,再一想,還是顯示農歷吧,畢竟這是咱們中國人自己的時間,
想想現在很多人不看日歷根本都都不知道農歷時間了。這段代碼也是從網上找來的,基本沒怎么改。
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();}/// <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];}使用: <table style=""><tr valign="bottom"> <td valign="bottom"> <%=ForignTimeNow%><br /></td>></tr><tr valign="bottom"><td valign="bottom"> <%=ChineseTimeNow %></td></tr></table>
url: http://greatverve.cnblogs.com/archive/2011/09/23/Lunar-calendar.html
總結
以上是生活随笔為你收集整理的ASP.NET显示农历时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机 小学数学应用题教学设计,小学数学
- 下一篇: 分式智能计算机在线计算,全能智能计算器