
C# 公农历算法 ChineseLunisolarCalendar的一点思路
代码 1 public partial class CalendarFive : System.Web.UI.Page
2 {
3 /// <summary>
4 /// 公历节
5 /// </summary>
6 string [,] holidays = new String[ 14 , 32 ];
7 /// <summary>
8 /// 农历节
9 /// </summary>
10 string [,] Chinaholidays = new String[ 14 , 32 ];
11 /// <summary>
12 /// 好日子
13 /// </summary>
14 string [,] GoodDays = new String[ 14 , 32 ];
15 /// <summary>
16 /// 周期性节日
17 /// </summary>
18 string [, ,] Weekholidays = new string [ 14 , 32 , 32 ];
19 /// <summary>
20 /// 中国历
21 /// </summary>
22 private static ChineseLunisolarCalendar ChinaCalendar = new ChineseLunisolarCalendar();
23
24 protected void Page_Load( object sender, EventArgs e)
25 {
26 // string aHoliday;
27 string aChinaHoliday = null ;
28
29 HtmlHead head = (HtmlHead)Page.Header;
30 // this.Calendar1.ToolTip = "缔友计算机信息技术有限有公司日历";
31 this .Label1.Text = " 您当前没有选取任何日期... " ;
32 Page.Header.Title = " 缔友计算机信息技术有限有公司日历 " ;
33 HtmlMeta meta = new HtmlMeta();
34 meta.Name = " Author " ;
35 meta.Attributes.Add( " Content " , " geovindu,软件开发,个性化服务 " );
36 head.Controls.AddAt( 1 , meta);
37
38 HtmlMeta key = new HtmlMeta();
39 key.Name = " keywords " ;
40 key.Attributes.Add( " Content " , " Geovin Du,涂聚文,捷为工作室,江西省吉安市永丰县市政花园 " );
41 head.Controls.AddAt( 2 , key);
42
43 HtmlLink ico = new HtmlLink();
44 ico.Href = " ~/favicon.ico " ;
45 ico.Attributes.Add( " rel " , " icon " );
46 ico.Attributes.Add( " type " , " image/ico " );
47 head.Controls.AddAt( 3 , ico);
48
49
50 HtmlLink bookmark = new HtmlLink();
51 bookmark.Href = " ~/favicon.ico " ;
52 bookmark.Attributes.Add( " rel " , " bookmark " );
53 // bookmark.Attributes.Add("type", "image/ico");
54 head.Controls.AddAt( 4 , bookmark);
55
56 HtmlLink shortcut = new HtmlLink();
57 shortcut.Href = " ~/favicon.ico " ;
58 shortcut.Attributes.Add( " rel " , " shortcut icon " );
59 shortcut.Attributes.Add( " type " , " image/x-icon " );
60 head.Controls.AddAt( 5 , shortcut);
61
62 HtmlMeta description = new HtmlMeta();
63 description.Name = " description " ;
64 description.Attributes.Add( " Content " , " 涂聚文,缔友计算机信息技术有限公司,经营信息流,物流,人力资源流,资本流的系统解决方案的开发与设计和服务 " );
65 head.Controls.AddAt( 6 , description);
66
67 // 国际节
68 holidays[ 1 , 1 ] = " 元旦 " ;
69 holidays[ 2 , 14 ] = " 情人节 " ;
70 holidays[ 3 , 8 ] = " 妇女节 " ;
71 holidays[ 3 , 15 ] = " 消费者权益日 " ;
72 holidays[ 4 , 1 ] = " 愚人节 " ;
73 holidays[ 5 , 1 ] = " 劳动节 " ;
74 holidays[ 5 , 4 ] = " 青年节 " ;
75 holidays[ 6 , 1 ] = " 儿童节 " ;
76 holidays[ 7 , 1 ] = " 党的生日 " ;
77 holidays[ 8 , 1 ] = " 建军节 " ;
78 holidays[ 9 , 10 ] = " 教师节 " ;
79 holidays[ 10 , 1 ] = " 国庆节 " ;
80 holidays[ 12 , 24 ] = " 平安夜 " ;
81 holidays[ 12 , 25 ] = " 圣诞节 " ;
82
83 // 中国节
84 Chinaholidays[ 1 , 1 ] = " 春节 " ;
85 Chinaholidays[ 1 , 15 ] = " 元宵节 " ;
86 // Chinaholidays[2, 21] = "清明节"; 是节气,是一定的规律,但不是规定那一个农历日期
87 Chinaholidays[ 5 , 5 ] = " 端午节 " ;
88
89 Chinaholidays[ 7 , 7 ] = " 七夕 " ;
90 Chinaholidays[ 7 , 15 ] = " 鬼节 " ;
91 Chinaholidays[ 8 , 15 ] = " 中秋节 " ;
92 Chinaholidays[ 9 , 9 ] = " 重阳 " ;
93 Chinaholidays[ 12 , 8 ] = " 腊八节 " ;
94 Chinaholidays[ 12 , 23 ] = " 小年 " ;
95 Chinaholidays[ 2 , 14 ] = " 涂聚文生日 " ;
96 // Chinaholidays[12, 30] = "除夕"; 是一年的最后一天
97 // 周期节日
98 Weekholidays[ 5 , 2 , 1 ] = " 母亲节 " ;
99 Weekholidays[ 5 , 3 , 1 ] = " 全国助残日 " ;
100 Weekholidays[ 6 , 3 , 1 ] = " 父亲节 " ;
101 Weekholidays[ 9 , 3 , 3 ] = " 国际和平日 " ;
102 Weekholidays[ 9 , 4 , 1 ] = " 国际聋人节 " ;
103 Weekholidays[ 10 , 1 , 2 ] = " 国际住房日 " ;
104 Weekholidays[ 10 , 1 , 4 ] = " 国际减轻自然灾害日 " ;
105 Weekholidays[ 11 , 4 , 5 ] = " 感恩节 " ;
106
107
108
109 // 得到闰月月份
110 int isLeap = 0 ;
111 DateTime theday = DateTime.Today;
112 DateTime days = DateTime.Parse( " 2017-07-7 " );
113 DateTime days1 = DateTime.Parse( " 2013-2-7 " );
114 DateTime days2 = DateTime.Parse( " 2017-09-7 " );
115 DateTime days3 = DateTime.Parse( " 2016-09-15 " );
116 // 2009-10-03 // 2010-09-22 // 2011-09-12 // 2012-09-30 // 2013-09-19 // 2014-09-08 // 2015-09-27 // 2016-09-15 // 2017-10-04
117 // 2009-01-26 // 2010-02-13 // 2011-02-02 // 2013-02-09 // 2012-01-22 // 2017-01-26
118 this .Label1.Text = ChineseCalendarHelper.GetStemBranch(DateTime.Now) + " 年 " + ChineseCalendarHelper.GetMonth(DateTime.Now) + " 月 " + ChineseCalendarHelper.GetDay(DateTime.Now) + " 生肖: " + ChineseCalendarHelper.GetSX(DateTime.Now);
119 this .Label2.Text = " 今天农历日期: " + ChinaCalendar.GetYear(theday).ToString() + ChinaCalendar.GetMonth(theday).ToString() + ChinaCalendar.GetDayOfMonth(theday).ToString();
120 this .Label3.Text = days.ToString() + " 农历日期: " + ChinaCalendar.GetYear(days).ToString() + ChinaCalendar.GetMonth(days).ToString() + ChinaCalendar.GetDayOfMonth(days).ToString();
121 this .Label4.Text = ChinaCalendar.GetLeapMonth(days.Year).ToString() + ChinaCalendar.GetMonthsInYear(days.Year).ToString() + ChinaCalendar.IsLeapMonth(days.Year, days.Month).ToString() + ChinaCalendar.IsLeapDay(days.Year, days.Month, days.Day).ToString();
122
123 if (ChinaCalendar.IsLeapMonth(days.Year, days.Month) == true )
124 {
125 isLeap = ChinaCalendar.GetMonth(days);
126 }
127 if ((ChinaCalendar.GetMonth(days1) - 1 ) == isLeap && isLeap != 0 )
128 {
129 this .Label5.Text = days1.ToString() + " 农历闰月日期: " + ChinaCalendar.GetYear(days1).ToString() + (ChinaCalendar.GetMonth(days1) - 1 ).ToString() + ChinaCalendar.GetDayOfMonth(days1).ToString();
130 }
131 else
132 {
133 this .Label5.Text = days1.ToString() + " 农历日期: " + ChinaCalendar.GetYear(days1).ToString() + ChinaCalendar.GetMonth(days1).ToString() + ChinaCalendar.GetDayOfMonth(days1).ToString();
134 }
135 // this.Label5.Text = "2012-06-7农历日期:" + ChinaCalendar.GetYear(days1).ToString() + ChinaCalendar.GetMonth(days1).ToString() + ChinaCalendar.GetDayOfMonth(days1).ToString();
136 this .Label6.Text = ChinaCalendar.IsLeapYear(days1.Year).ToString() + ChinaCalendar.GetLeapMonth(days1.Year).ToString() + ChinaCalendar.GetMonthsInYear(days1.Year).ToString() + ChinaCalendar.IsLeapMonth(days1.Year, days1.Month).ToString() + ChinaCalendar.IsLeapDay(days1.Year, days1.Month, days1.Day).ToString();
137 if (ChinaCalendar.GetMonth(days2) > isLeap && isLeap != 0 )
138 {
139 this .Label7.Text = days2.ToString() + " 农历日期: " + ChinaCalendar.GetYear(days2).ToString() + (ChinaCalendar.GetMonth(days2) - 1 ).ToString() + ChinaCalendar.GetDayOfMonth(days2).ToString();
140 }
141 else
142 {
143 this .Label7.Text = days2.ToString() + " 农历日期: " + ChinaCalendar.GetYear(days2).ToString() + ChinaCalendar.GetMonth(days2).ToString() + ChinaCalendar.GetDayOfMonth(days2).ToString();
144 }
145 this .Label8.Text = isLeap.ToString();
146 this .Label9.Text = days1.ToString() + " 共多少天: " + ChinaCalendar.GetDaysInMonth(days1.Year, days1.Month).ToString();
147 int d = 0 ;
148 int y = ChinaCalendar.GetYear(days3);
149 int m = ChinaCalendar.GetMonth(days3);
150 int s = ChinaCalendar.GetDayOfMonth(days3);
151 #region 闰年判断
152 if (ChinaCalendar.IsLeapYear(y, ChineseLunisolarCalendar.CurrentEra) == true )
153 {
154 d = ChinaCalendar.GetDaysInMonth(y, m, ChineseLunisolarCalendar.CurrentEra);
155 this .Label11.Text = " true " ;
156 if (m == 13 && d == ChinaCalendar.GetDayOfMonth(days3))
157 {
158 this .Label10.Text = days3.ToString() + " 闰年农历日期除夕: " + ChinaCalendar.GetYear(days3).ToString() + (ChinaCalendar.GetMonth(days3) - 1 ).ToString() + ChinaCalendar.GetDayOfMonth(days3).ToString();
159 }
160 // 闰月
161 if (ChinaCalendar.IsLeapMonth(y, m) == true )
162 {
163 isLeap = m;
164 }
165 // 当时闰月之上的农历月份要减一来算节日
166 if (m > isLeap && isLeap != 0 )
167 {
168 aChinaHoliday = Chinaholidays[m - 1 , s];
169
170 // 显示中国节日
171 if (aChinaHoliday != null )
172 {
173 // Label aLabel = new Label();
174 // aLabel.Font.Size = 10;
175 // aLabel.ForeColor = System.Drawing.Color.Red;
176 // aLabel.Text = "<br>[" + aChinaHoliday + "]";
177 // e.Cell.Controls.Add(aLabel);
178 this .Label12.Text = aChinaHoliday;
179 }
180 }
181 else
182 {
183 aChinaHoliday = Chinaholidays[m, s];
184 // 显示中国节日
185 if (aChinaHoliday != null )
186 {
187 // Label aLabel = new Label();
188 // aLabel.Font.Size = 10;
189 // aLabel.ForeColor = System.Drawing.Color.Red;
190 // aLabel.Text = "<br>[" + aChinaHoliday + "]";
191 // e.Cell.Controls.Add(aLabel);
192 this .Label12.Text = aChinaHoliday;
193 }
194 }
195 }
196 else
197 {
198 d = ChinaCalendar.GetDaysInMonth(y, m, ChineseLunisolarCalendar.CurrentEra);
199 if (d == s && m == 12 )
200 {
201 this .Label10.Text = days3.ToString() + " 农历日期除夕: " + ChinaCalendar.GetYear(days3).ToString() + (ChinaCalendar.GetMonth(days3) ).ToString() + ChinaCalendar.GetDayOfMonth(days3).ToString();
202 }
203 aChinaHoliday = Chinaholidays[m, s];
204 // 显示中国节日
205 if (aChinaHoliday != null )
206 {
207 // Label aLabel = new Label();
208 // aLabel.Font.Size = 10;
209 // aLabel.ForeColor = System.Drawing.Color.Red;
210 // aLabel.Text = "<br>[" + aChinaHoliday + "]";
211 // e.Cell.Controls.Add(aLabel);
212 this .Label12.Text = aChinaHoliday;
213 }
214 }
215 #endregion
216
217 if (ChinaCalendar.IsLeapMonth(days3.Year, days3.Month, ChineseLunisolarCalendar.CurrentEra) == true )
218 {
219 isLeap = ChinaCalendar.GetMonth(days3);
220 }
221 if ((ChinaCalendar.GetMonth(days3) - 1 ) == isLeap && isLeap != 0 )
222 {
223 this .Label10.Text = days3.ToString() + " 农历闰月日期: " + ChinaCalendar.GetYear(days3).ToString() + (ChinaCalendar.GetMonth(days3) - 1 ).ToString() + ChinaCalendar.GetDayOfMonth(days3).ToString();
224 }
225 else
226 {
227 if (ChinaCalendar.GetMonth(days2) == 12 && ChinaCalendar.GetDayOfMonth(days3) == 30 )
228 {
229 d = ChinaCalendar.GetDaysInMonth(days3.Year - 1 , days3.Month);
230 }
231
232
233
234 if (d == ChinaCalendar.GetDayOfMonth(days3))
235 {
236 // this.Label10.Text = days3.ToString() + "农历日期" + ChinaCalendar.GetYear(days3).ToString() + ChinaCalendar.GetMonth(days3).ToString() + ChinaCalendar.GetDayOfMonth(days3).ToString();
237 }
238 else
239 {
240 this .Label10.Text = days3.ToString() + " 农历日期 " + ChinaCalendar.GetYear(days3).ToString() + ChinaCalendar.GetMonth(days3).ToString() + ChinaCalendar.GetDayOfMonth(days3).ToString() + " total: " + d.ToString();
241 }
242 }
243 }
244 }
245
Demo: .aspx
這是用Winform 做的萬年曆,可以編輯各種節日。