网页前端设计

http://www.86y.org

搜索文章

ASP.NET显示农历时间

用声音读出全文关注我吧
 2011/9/23 15:10:37 阅读次数:3338

今天看了下之前写的网页,突然想起应该加个时间的。于是准备就直接DateTime.Now一下,再一想,还是显示农历吧,毕竟这是咱们中国人自己的时间,想想现在很多人不看日历根本都都不知道农历时间了,真佩服我们中华民族的人是多么牛了,一个斗大字不认识的人都能准确的知道今天的农历时间。我们的文化何止博大精深啊!

这段代码也是从网上找来的,基本没怎么改。
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];
        }

前台:

 <table >
 <tr valign="bottom">
<td valign="bottom"> <%=ForignTimeNow%><br /></td>>
   </tr>
   <tr valign="bottom">
  <td valign="bottom"> <%=ChineseTimeNow %></td>
 </tr>
 </table>

大家有什么问题或技术上的想法可以在此与大家分享,也可以加入前端爱好者QQ群(141999928)一起学习进步:【幸凡前端技术交流群】
0

如果您觉得本文的内容对您的学习有所帮助,捐赠与共勉,支付宝(左)或微信(右)

阅读全文内容关闭