博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

基于asp.net显示日期(含农历)的实例

Posted on 2006-10-21 10:46  sun.Lei  阅读(3107)  评论(4编辑  收藏  举报
该实例是基于asp.net从服务端或取日期,并转换为中国农历,使用了.NET中自带的农历处理类,优点处理方便!具体代码如下:

首先可以封装成一个类来处理核心部分

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Globalization;
 5
 6namespace chinese.D0086.Date
 7{
 8    public class chinaDate
 9    {
10        public string cDate()
11        {
12            ChineseLunisolarCalendar l = new ChineseLunisolarCalendar();
13            DateTime dt = DateTime.Today; //转换当日的日期
14            //dt = new DateTime(2006, 1,29);//农历2006年大年初一(测试用),也可指定日期转换
15            string[] aMonth ={"","正月""二月""三月""四月""五月""六月""七月""八月""九月""十月""十一月""腊月""腊月" };
16            //a10表示日期的十位!
17            string[] a10 ="""""廿""" };
18            string[] aDigi ="""""""""""""""""""" };
19            string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
20            int iYear, iMonth, iDay;
21            iYear = l.GetYear(dt);
22            iMonth = l.GetMonth(dt);
23            iDay = l.GetDayOfMonth(dt);
24
25            //Format Year
26            sYearArab = iYear.ToString();
27            for (int i = 0; i < sYearArab.Length; i++)
28            {
29                sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
30            }

31
32            //Format Month
33            int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月
34
35/*闰月可以出现在一年的任何月份之后。例如,GetMonth 方法返回一个介于 1 到 13 之间的数字来表示与指定日期关联的月份。如果在一年的八月和九月之间有一个闰月,则 GetMonth 方法为八月返回 8,为闰八月返回 9,为九月返回 10。*/  
36        
37            if (iLeapMonth > 0 && iMonth<=iLeapMonth)
38            {
39                //for (int i = iLeapMonth + 1; i < 13; i++) aMonth[i] = aMonth[i - 1];
40                aMonth[iLeapMonth] = "" + aMonth[iLeapMonth-1];
41                sMonth = aMonth[l.GetMonth(dt)];
42            }

43            else if (iLeapMonth > 0 && iMonth > iLeapMonth)
44            {
45                sMonth = aMonth[l.GetMonth(dt) - 1];
46            }

47            else
48            {
49                sMonth = aMonth[l.GetMonth(dt)];
50            }

51
52
53            //Format Day
54            sDay10 = a10[iDay / 10];
55            sDay1 = aDigi[(iDay % 10)];
56            sDay = sDay10 + sDay1;
57
58            if (iDay == 10) sDay = "初十";
59            if (iDay == 20) sDay = "二十";
60            if (iDay == 30) sDay = "三十";
61
62            //Format Lunar Date
63            //sLuniSolarDate = dt.Year+"年"+dt.Month+"月"+dt.Day+"日 "+Weeks(dt.DayOfWeek.ToString())+" 农历" + sYear + "年" + sMonth + sDay;
64            sLuniSolarDate = dt.Year + "" + dt.Month + "" + dt.Day + "日 " + Weeks(dt.DayOfWeek.ToString()) + " 农历" + sMonth + sDay;
65            return sLuniSolarDate;
66           
67        }

68        private string Weeks(string Weeken)
69        {
70            switch (Weeken)
71            {
72                case "Monday":
73                    return "星期一";
74                    break;
75                case "Tuesday":
76                    return "星期二";
77                    break;
78                case "Wednesday":
79                    return "星期三";
80                    break;
81                case "Thursday":
82                    return "星期四";
83                    break;
84                case "Friday":
85                    return "星期五";
86                    break;
87                case "Saturday":
88                    return "星期六";
89                    break;
90                case "Sunday":
91                    return "星期日";
92                    break;
93                default:
94                    return " ";
95            }

96            
97        }

98    }

99}


在asp.net页面中直接调用即可,
 1
 2
 3using chinese.D0086.Date;
 4
 5public partial class view1 : System.Web.UI.Page
 6{
 7    protected void Page_Load(object sender, EventArgs e)
 8    {
 9        chinaDate s = new chinaDate();
10        Response.Write(s.cDate());
11    }

12
13}


Welcome To sun.Lei Blog!!