随笔 - 19, 文章 - 2, 评论 - 90, 阅读 - 49662
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   sun.Lei  阅读(3123)  评论(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}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述

Welcome To sun.Lei Blog!!

点击右上角即可分享
微信分享提示