c# 日历类的实现代码,具体如下所示:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
|
using system;
namespace dotnet.utilities
{
/// <summary>
/// 农历属性
/// </summary>
public class cndate
{
/// <summary>
/// 农历年(整型)
/// </summary>
public int cnintyear = 0;
/// <summary>
/// 农历月份(整型)
/// </summary>
public int cnintmonth = 0;
/// <summary>
/// 农历天(整型)
/// </summary>
public int cnintday = 0;
/// <summary>
/// 农历年(支干)
/// </summary>
public string cnstryear = "" ;
/// <summary>
/// 农历月份(字符)
/// </summary>
public string cnstrmonth = "" ;
/// <summary>
/// 农历天(字符)
/// </summary>
public string cnstrday = "" ;
/// <summary>
/// 农历属象
/// </summary>
public string cnanm = "" ;
/// <summary>
/// 二十四节气
/// </summary>
public string cnsolarterm = "" ;
/// <summary>
/// 阴历节日
/// </summary>
public string cnftvl = "" ;
/// <summary>
/// 阳历节日
/// </summary>
public string cnftvs = "" ;
}
/// <summary>
/// 公历转农历
/// </summary>
public class chinadate
{
#region 私有方法
private static long [] lunarinfo = new long [] { 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554,
0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0,
0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550,
0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0,
0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,
0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0,
0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,
0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0,
0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0,
0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520,
0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };
private static int [] year20 = new int [] { 1, 4, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
private static int [] year19 = new int [] { 0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
private static int [] year2000 = new int [] { 0, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
private static string [] nstr1 = new string [] { "" , "正" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" , "十" , "十一" , "十二" };
private static string [] gan = new string [] { "甲" , "乙" , "丙" , "丁" , "戊" , "己" , "庚" , "辛" , "壬" , "癸" };
private static string [] zhi = new string [] { "子" , "丑" , "寅" , "卯" , "辰" , "巳" , "午" , "未" , "申" , "酉" , "戌" , "亥" };
private static string [] animals = new string [] { "鼠" , "牛" , "虎" , "兔" , "龙" , "蛇" , "马" , "羊" , "猴" , "鸡" , "狗" , "猪" };
private static string [] solarterm = new string [] { "小寒" , "大寒" , "立春" , "雨水" , "惊蛰" , "春分" , "清明" , "谷雨" , "立夏" , "小满" , "芒种" , "夏至" , "小暑" , "大暑" , "立秋" , "处暑" , "白露" , "秋分" , "寒露" , "霜降" , "立冬" , "小雪" , "大雪" , "冬至" };
private static int [] sterminfo = { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
private static string [] lftv = new string [] { "0101农历春节" , "0202 龙抬头节" , "0115 元宵节" , "0505 端午节" , "0707 七夕情人节" , "0815 中秋节" , "0909 重阳节" , "1208 腊八节" , "1114 李君先生生日" , "1224 小年" , "0100除夕" };
private static string [] sftv = new string [] { "0101 新年元旦" ,
"0202 世界湿地日" ,
"0207 国际声援南非日" ,
"0210 国际气象节" ,
"0214 情人节" ,
"0301 国际海豹日" ,
"0303 全国爱耳日" ,
"0308 国际妇女节" ,
"0312 植树节 孙中山逝世纪念日" ,
"0314 国际警察日" ,
"0315 国际消费者权益日" ,
"0317 中国国医节 国际航海日" ,
"0321 世界森林日 消除种族歧视国际日" ,
"0321 世界儿歌日" ,
"0322 世界水日" ,
"0323 世界气象日" ,
"0324 世界防治结核病日" ,
"0325 全国中小学生安全教育日" ,
"0330 巴勒斯坦国土日" ,
"0401 愚人节 全国爱国卫生运动月(四月) 税收宣传月(四月)" ,
"0407 世界卫生日" ,
"0422 世界地球日" ,
"0423 世界图书和版权日" ,
"0424 亚非新闻工作者日" ,
"0501 国际劳动节" ,
"0504 中国五四青年节" ,
"0505 碘缺乏病防治日" ,
"0508 世界红十字日" ,
"0512 国际护士节" ,
"0515 国际家庭日" ,
"0517 世界电信日" ,
"0518 国际博物馆日" ,
"0520 全国学生营养日" ,
"0523 国际牛奶日" ,
"0531 世界无烟日" ,
"0601 国际儿童节" ,
"0605 世界环境日" ,
"0606 全国爱眼日" ,
"0617 防治荒漠化和干旱日" ,
"0623 国际奥林匹克日" ,
"0625 全国土地日" ,
"0626 国际反毒品日" ,
"0701 中国*建党日 世界建筑日" ,
"0702 国际体育记者日" ,
"0707 中国人民抗日战争纪念日" ,
"0711 世界人口日" ,
"0730 非洲妇女日" ,
"0801 中国建军节" ,
"0808 中国男子节(爸爸节)" ,
"0815 日本正式宣布无条件投降日" ,
"0908 国际扫盲日 国际新闻工作者日" ,
"0910 教师节" ,
"0914 世界清洁地球日" ,
"0916 国际臭氧层保护日" ,
"0918 九·一八事变纪念日" ,
"0920 全国爱牙日" ,
"0927 世界旅游日" ,
"1001 国庆节 世界音乐日 国际老人节" ,
"1001 国际音乐日" ,
"1002 国际和平与**斗争日" ,
"1004 世界动物日" ,
"1008 全国高血压日" ,
"1008 世界视觉日" ,
"1009 世界邮政日 万国邮联日" ,
"1010 辛亥革命纪念日 世界精神卫生日" ,
"1013 世界保健日 国际教师节" ,
"1014 世界标准日" ,
"1015 国际盲人节(白手杖节)" ,
"1016 世界粮食日" ,
"1017 世界消除贫困日" ,
"1022 世界传统医药日" ,
"1024 联合国日 世界发展信息日" ,
"1031 世界勤俭日" ,
"1107 十月*革命纪念日" ,
"1108 中国记者日" ,
"1109 全国消防安全宣传教育日" ,
"1110 世界青年节" ,
"1111 国际科学与和平周(本日所属的一周)" ,
"1112 孙中山诞辰纪念日" ,
"1114 世界糖尿病日" ,
"1117 国际大学生节 世界学生节" ,
"1121 世界问候日 世界电视日" ,
"1129 国际声援巴勒斯坦人民国际日" ,
"1201 世界艾滋病日" ,
"1203 世界残疾人日" ,
"1205 国际经济和社会发展志愿人员日" ,
"1208 国际儿童电视日" ,
"1209 世界足球日" ,
"1210 世界*日" ,
"1212 西安事变纪念日" ,
"1213 南京大*(1937年)纪念日!紧记血泪史!" ,
"1221 国际篮球日" ,
"1224 平安夜" ,
"1225 圣诞节" ,
"1226 *诞辰" ,
"1229 国际生物多样性日" };
/// <summary>
/// 传回农历y年的总天数
/// </summary>
private static int lyeardays( int y)
{
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1)
{
if ((lunarinfo[y - 1900] & i) != 0)
sum += 1;
}
return (sum + leapdays(y));
}
/// <summary>
/// 传回农历y年闰月的天数
/// </summary>
private static int leapdays( int y)
{
if (leapmonth(y) != 0)
{
if ((lunarinfo[y - 1900] & 0x10000) != 0)
return 30;
else
return 29;
}
else
return 0;
}
/// <summary>
/// 传回农历y年闰哪个月 1-12 , 没闰传回 0
/// </summary>
private static int leapmonth( int y)
{
return ( int )(lunarinfo[y - 1900] & 0xf);
}
/// <summary>
/// 传回农历y年m月的总天数
/// </summary>
private static int monthdays( int y, int m)
{
if ((lunarinfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
/// <summary>
/// 传回农历y年的生肖
/// </summary>
private static string animalsyear( int y)
{
return animals[(y - 4) % 12];
}
/// <summary>
/// 传入月日的offset 传回干支,0=甲子
/// </summary>
private static string cyclicalm( int num)
{
return (gan[num % 10] + zhi[num % 12]);
}
/// <summary>
/// 传入offset 传回干支, 0=甲子
/// </summary>
private static string cyclical( int y)
{
int num = y - 1900 + 36;
return (cyclicalm(num));
}
/// <summary>
/// 传出农历.year0 .month1 .day2 .yearcyl3 .moncyl4 .daycyl5 .isleap6
/// </summary>
private long [] lunar( int y, int m)
{
long [] nongdate = new long [7];
int i = 0, temp = 0, leap = 0;
datetime basedate = new datetime(1900 + 1900, 2, 31);
datetime objdate = new datetime(y + 1900, m + 1, 1);
timespan ts = objdate - basedate;
long offset = ( long )ts.totaldays;
if (y < 2000)
offset += year19[m - 1];
if (y > 2000)
offset += year20[m - 1];
if (y == 2000)
offset += year2000[m - 1];
nongdate[5] = offset + 40;
nongdate[4] = 14;
for (i = 1900; i < 2050 && offset > 0; i++)
{
temp = lyeardays(i);
offset -= temp;
nongdate[4] += 12;
}
if (offset < 0)
{
offset += temp;
i--;
nongdate[4] -= 12;
}
nongdate[0] = i;
nongdate[3] = i - 1864;
leap = leapmonth(i); // 闰哪个月
nongdate[6] = 0;
for (i = 1; i < 13 && offset > 0; i++)
{
// 闰月
if (leap > 0 && i == (leap + 1) && nongdate[6] == 0)
{
--i;
nongdate[6] = 1;
temp = leapdays(( int )nongdate[0]);
}
else
{
temp = monthdays(( int )nongdate[0], i);
}
// 解除闰月
if (nongdate[6] == 1 && i == (leap + 1))
nongdate[6] = 0;
offset -= temp;
if (nongdate[6] == 0)
nongdate[4]++;
}
if (offset == 0 && leap > 0 && i == leap + 1)
{
if (nongdate[6] == 1)
{
nongdate[6] = 0;
}
else
{
nongdate[6] = 1;
--i;
--nongdate[4];
}
}
if (offset < 0)
{
offset += temp;
--i;
--nongdate[4];
}
nongdate[1] = i;
nongdate[2] = offset + 1;
return nongdate;
}
/// <summary>
/// 传出y年m月d日对应的农历.year0 .month1 .day2 .yearcyl3 .moncyl4 .daycyl5 .isleap6
/// </summary>
private static long [] calelement( int y, int m, int d)
{
long [] nongdate = new long [7];
int i = 0, temp = 0, leap = 0;
datetime basedate = new datetime(1900, 1, 31);
datetime objdate = new datetime(y, m, d);
timespan ts = objdate - basedate;
long offset = ( long )ts.totaldays;
nongdate[5] = offset + 40;
nongdate[4] = 14;
for (i = 1900; i < 2050 && offset > 0; i++)
{
temp = lyeardays(i);
offset -= temp;
nongdate[4] += 12;
}
if (offset < 0)
{
offset += temp;
i--;
nongdate[4] -= 12;
}
nongdate[0] = i;
nongdate[3] = i - 1864;
leap = leapmonth(i); // 闰哪个月
nongdate[6] = 0;
for (i = 1; i < 13 && offset > 0; i++)
{
// 闰月
if (leap > 0 && i == (leap + 1) && nongdate[6] == 0)
{
--i;
nongdate[6] = 1;
temp = leapdays(( int )nongdate[0]);
}
else
{
temp = monthdays(( int )nongdate[0], i);
}
// 解除闰月
if (nongdate[6] == 1 && i == (leap + 1))
nongdate[6] = 0;
offset -= temp;
if (nongdate[6] == 0)
nongdate[4]++;
}
if (offset == 0 && leap > 0 && i == leap + 1)
{
if (nongdate[6] == 1)
{
nongdate[6] = 0;
}
else
{
nongdate[6] = 1;
--i;
--nongdate[4];
}
}
if (offset < 0)
{
offset += temp;
--i;
--nongdate[4];
}
nongdate[1] = i;
nongdate[2] = offset + 1;
return nongdate;
}
private static string getchinadate( int day)
{
string a = "" ;
if (day == 10)
return "初十" ;
if (day == 20)
return "二十" ;
if (day == 30)
return "三十" ;
int two = ( int )((day) / 10);
if (two == 0)
a = "初" ;
if (two == 1)
a = "十" ;
if (two == 2)
a = "廿" ;
if (two == 3)
a = "三" ;
int one = ( int )(day % 10);
switch (one)
{
case 1:
a += "一" ;
break ;
case 2:
a += "二" ;
break ;
case 3:
a += "三" ;
break ;
case 4:
a += "四" ;
break ;
case 5:
a += "五" ;
break ;
case 6:
a += "六" ;
break ;
case 7:
a += "七" ;
break ;
case 8:
a += "八" ;
break ;
case 9:
a += "九" ;
break ;
}
return a;
}
private static datetime sterm( int y, int n)
{
double ms = 31556925974.7 * (y - 1900);
double ms1 = sterminfo[n];
datetime offdate = new datetime(1900, 1, 6, 2, 5, 0);
offdate = offdate.addmilliseconds(ms);
offdate = offdate.addminutes(ms1);
return offdate;
}
static string formatdate( int m, int d)
{
return string .format( "{0:00}{1:00}" , m, d);
}
#endregion
#region 公有方法
/// <summary>
/// 传回公历y年m月的总天数
/// </summary>
public static int getdaysbymonth( int y, int m)
{
int [] days = new int [] { 31, datetime.isleapyear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return days[m - 1];
}
/// <summary>
/// 根据日期值获得周一的日期
/// </summary>
/// <param name="dt">输入日期</param>
/// <returns>周一的日期</returns>
public static datetime getmondaydatebydate(datetime dt)
{
double d = 0;
switch (( int )dt.dayofweek)
{
//case 1: d = 0; break;
case 2: d = -1; break ;
case 3: d = -2; break ;
case 4: d = -3; break ;
case 5: d = -4; break ;
case 6: d = -5; break ;
case 0: d = -6; break ;
}
return dt.adddays(d);
}
/// <summary>
/// 获取农历
/// </summary>
public static cndate getchinadate(datetime dt)
{
cndate cd = new cndate();
int year = dt.year;
int month = dt.month;
int date = dt.day;
long [] l = calelement(year, month, date);
cd.cnintyear = ( int )l[0];
cd.cnintmonth = ( int )l[1];
cd.cnintday = ( int )l[2];
cd.cnstryear = cyclical(year);
cd.cnanm = animalsyear(year);
cd.cnstrmonth = nstr1[( int )l[1]];
cd.cnstrday = getchinadate(( int )(l[2]));
string smd = dt.tostring( "mmdd" );
string lmd = formatdate(cd.cnintmonth, cd.cnintday);
for ( int i = 0; i < solarterm.length; i++)
{
string s1 = sterm(dt.year, i).tostring( "mmdd" );
if (s1.equals(dt.tostring( "mmdd" )))
{
cd.cnsolarterm = solarterm[i];
break ;
}
}
foreach ( string s in sftv)
{
string s1 = s.substring(0, 4);
if (s1.equals(smd))
{
cd.cnftvs = s.substring(4, s.length - 4);
break ;
}
}
foreach ( string s in lftv)
{
string s1 = s.substring(0, 4);
if (s1.equals(lmd))
{
cd.cnftvl = s.substring(4, s.length - 4);
break ;
}
}
dt = dt.adddays(1);
year = dt.year;
month = dt.month;
date = dt.day;
l = calelement(year, month, date);
lmd = formatdate(( int )l[1], ( int )l[2]);
if (lmd.equals( "0101" )) cd.cnftvl = "除夕" ;
return cd;
}
#endregion
}
/// <summary>
/// 中国日历
/// </summary>
//-------------------------------------------------------------------------------
//调用:
//chinesecalendar c = new chinesecalendar(new datetime(1990, 01, 15));
//stringbuilder dayinfo = new stringbuilder();
//dayinfo.append("阳历:" + c.datestring + "\r\n");
//dayinfo.append("农历:" + c.chinesedatestring + "\r\n");
//dayinfo.append("星期:" + c.weekdaystr);
//dayinfo.append("时辰:" + c.chinesehour + "\r\n");
//dayinfo.append("属相:" + c.animalstring + "\r\n");
//dayinfo.append("节气:" + c.chinesetwentyfourday + "\r\n");
//dayinfo.append("前一个节气:" + c.chinesetwentyfourprevday + "\r\n");
//dayinfo.append("下一个节气:" + c.chinesetwentyfournextday + "\r\n");
//dayinfo.append("节日:" + c.dateholiday + "\r\n");
//dayinfo.append("干支:" + c.ganzhidatestring + "\r\n");
//dayinfo.append("星宿:" + c.chineseconstellation + "\r\n");
//dayinfo.append("星座:" + c.constellation + "\r\n");
//-------------------------------------------------------------------------------
public class chinesecalendar
{
#region 内部结构
/// <summary>
/// 阳历
/// </summary>
private struct solarholidaystruct
{
public int month;
public int day;
public int recess; //假期长度
public string holidayname;
public solarholidaystruct( int month, int day, int recess, string name)
{
month = month;
day = day;
recess = recess;
holidayname = name;
}
}
/// <summary>
/// 农历
/// </summary>
private struct lunarholidaystruct
{
public int month;
public int day;
public int recess;
public string holidayname;
public lunarholidaystruct( int month, int day, int recess, string name)
{
month = month;
day = day;
recess = recess;
holidayname = name;
}
}
private struct weekholidaystruct
{
public int month;
public int weekatmonth;
public int weekday;
public string holidayname;
public weekholidaystruct( int month, int weekatmonth, int weekday, string name)
{
month = month;
weekatmonth = weekatmonth;
weekday = weekday;
holidayname = name;
}
}
#endregion
#region 内部变量
private datetime _date;
private datetime _datetime;
private int _cyear;
private int _cmonth;
private int _cday;
private bool _cisleapmonth; //当月是否闰月
private bool _cisleapyear; //当年是否有闰月
#endregion
#region 基础数据
#region 基本常量
private const int minyear = 1900;
private const int maxyear = 2050;
private static datetime minday = new datetime(1900, 1, 30);
private static datetime maxday = new datetime(2049, 12, 31);
private const int ganzhistartyear = 1864; //干支计算起始年
private static datetime ganzhistartday = new datetime(1899, 12, 22); //起始日
private const string hznum = "零一二三四五六七八九" ;
private const int animalstartyear = 1900; //1900年为鼠年
private static datetime chineseconstellationreferday = new datetime(2007, 9, 13); //28星宿参考值,本日为角
#endregion
#region 阴历数据
/// <summary>
/// 来源于网上的农历数据
/// </summary>
/// <remarks>
/// 数据结构如下,共使用17位数据
/// 第17位:表示闰月天数,0表示29天 1表示30天
/// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
/// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
///</remarks>
private static int [] lunardatearray = new int []{
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
0x14b63
};
#endregion
#region 星座名称
private static string [] _constellationname =
{
"白羊座" , "金牛座" , "双子座" ,
"巨蟹座" , "狮子座" , "处女座" ,
"天秤座" , "天蝎座" , "射手座" ,
"摩羯座" , "水瓶座" , "双鱼座"
};
#endregion
#region 二十四节气
private static string [] _lunarholidayname =
{
"小寒" , "大寒" , "立春" , "雨水" ,
"惊蛰" , "春分" , "清明" , "谷雨" ,
"立夏" , "小满" , "芒种" , "夏至" ,
"小暑" , "大暑" , "立秋" , "处暑" ,
"白露" , "秋分" , "寒露" , "霜降" ,
"立冬" , "小雪" , "大雪" , "冬至"
};
#endregion
#region 二十八星宿
private static string [] _chineseconstellationname =
{
//四 五 六 日 一 二 三
"角木蛟" , "亢金龙" , "女土蝠" , "房日兔" , "心月狐" , "尾火虎" , "箕水豹" ,
"斗木獬" , "牛金牛" , "氐土貉" , "虚日鼠" , "危月燕" , "室火猪" , "壁水獝" ,
"奎木狼" , "娄金狗" , "胃土彘" , "昴日鸡" , "毕月乌" , "觜火猴" , "参水猿" ,
"井木犴" , "鬼金羊" , "柳土獐" , "星日马" , "张月鹿" , "翼火蛇" , "轸水蚓"
};
#endregion
#region 节气数据
private static string [] solarterm = new string [] { "小寒" , "大寒" , "立春" , "雨水" , "惊蛰" , "春分" , "清明" , "谷雨" , "立夏" , "小满" , "芒种" , "夏至" , "小暑" , "大暑" , "立秋" , "处暑" , "白露" , "秋分" , "寒露" , "霜降" , "立冬" , "小雪" , "大雪" , "冬至" };
private static int [] sterminfo = new int [] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
#endregion
#region 农历相关数据
private static string ganstr = "甲乙丙丁戊己庚辛壬癸" ;
private static string zhistr = "子丑寅卯辰巳午未申酉戌亥" ;
private static string animalstr = "鼠牛虎兔龙蛇马羊猴鸡狗猪" ;
private static string nstr1 = "日一二三四五六七八九" ;
private static string nstr2 = "初十廿卅" ;
private static string [] _monthstring =
{
"出错" , "正月" , "二月" , "三月" , "四月" , "五月" , "六月" , "七月" , "八月" , "九月" , "十月" , "十一月" , "腊月"
};
#endregion
#region 按公历计算的节日
private static solarholidaystruct[] sholidayinfo = new solarholidaystruct[]{
new solarholidaystruct(1, 1, 1, "元旦" ),
new solarholidaystruct(2, 2, 0, "世界湿地日" ),
new solarholidaystruct(2, 10, 0, "国际气象节" ),
new solarholidaystruct(2, 14, 0, "情人节" ),
new solarholidaystruct(3, 1, 0, "国际海豹日" ),
new solarholidaystruct(3, 5, 0, "学雷锋纪念日" ),
new solarholidaystruct(3, 8, 0, "妇女节" ),
new solarholidaystruct(3, 12, 0, "植树节 孙中山逝世纪念日" ),
new solarholidaystruct(3, 14, 0, "国际警察日" ),
new solarholidaystruct(3, 15, 0, "消费者权益日" ),
new solarholidaystruct(3, 17, 0, "中国国医节 国际航海日" ),
new solarholidaystruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日" ),
new solarholidaystruct(3, 22, 0, "世界水日" ),
new solarholidaystruct(3, 24, 0, "世界防治结核病日" ),
new solarholidaystruct(4, 1, 0, "愚人节" ),
new solarholidaystruct(4, 7, 0, "世界卫生日" ),
new solarholidaystruct(4, 22, 0, "世界地球日" ),
new solarholidaystruct(5, 1, 1, "劳动节" ),
new solarholidaystruct(5, 2, 1, "劳动节假日" ),
new solarholidaystruct(5, 3, 1, "劳动节假日" ),
new solarholidaystruct(5, 4, 0, "青年节" ),
new solarholidaystruct(5, 8, 0, "世界红十字日" ),
new solarholidaystruct(5, 12, 0, "国际护士节" ),
new solarholidaystruct(5, 31, 0, "世界无烟日" ),
new solarholidaystruct(6, 1, 0, "国际儿童节" ),
new solarholidaystruct(6, 5, 0, "世界环境保护日" ),
new solarholidaystruct(6, 26, 0, "国际禁毒日" ),
new solarholidaystruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日" ),
new solarholidaystruct(7, 11, 0, "世界人口日" ),
new solarholidaystruct(8, 1, 0, "建军节" ),
new solarholidaystruct(8, 8, 0, "中国男子节 父亲节" ),
new solarholidaystruct(8, 15, 0, "抗日战争胜利纪念" ),
new solarholidaystruct(9, 9, 0, "*逝世纪念" ),
new solarholidaystruct(9, 10, 0, "教师节" ),
new solarholidaystruct(9, 18, 0, "九·一八事变纪念日" ),
new solarholidaystruct(9, 20, 0, "国际爱牙日" ),
new solarholidaystruct(9, 27, 0, "世界旅游日" ),
new solarholidaystruct(9, 28, 0, "孔子诞辰" ),
new solarholidaystruct(10, 1, 1, "国庆节 国际音乐日" ),
new solarholidaystruct(10, 2, 1, "国庆节假日" ),
new solarholidaystruct(10, 3, 1, "国庆节假日" ),
new solarholidaystruct(10, 6, 0, "老人节" ),
new solarholidaystruct(10, 24, 0, "联合国日" ),
new solarholidaystruct(11, 10, 0, "世界青年节" ),
new solarholidaystruct(11, 12, 0, "孙中山诞辰纪念" ),
new solarholidaystruct(12, 1, 0, "世界艾滋病日" ),
new solarholidaystruct(12, 3, 0, "世界残疾人日" ),
new solarholidaystruct(12, 20, 0, "澳门回归纪念" ),
new solarholidaystruct(12, 24, 0, "平安夜" ),
new solarholidaystruct(12, 25, 0, "圣诞节" ),
new solarholidaystruct(12, 26, 0, "*诞辰纪念" )
};
#endregion
#region 按农历计算的节日
private static lunarholidaystruct[] lholidayinfo = new lunarholidaystruct[]{
new lunarholidaystruct(1, 1, 1, "春节" ),
new lunarholidaystruct(1, 15, 0, "元宵节" ),
new lunarholidaystruct(5, 5, 0, "端午节" ),
new lunarholidaystruct(7, 7, 0, "七夕情人节" ),
new lunarholidaystruct(7, 15, 0, "中元节 盂兰盆节" ),
new lunarholidaystruct(8, 15, 0, "中秋节" ),
new lunarholidaystruct(9, 9, 0, "重阳节" ),
new lunarholidaystruct(12, 8, 0, "腊八节" ),
new lunarholidaystruct(12, 23, 0, "北方小年(扫房)" ),
new lunarholidaystruct(12, 24, 0, "南方小年(掸尘)" ),
//new lunarholidaystruct(12, 30, 0, "除夕") //注意除夕需要其它方法进行计算
};
#endregion
#region 按某月第几个星期几
private static weekholidaystruct[] wholidayinfo = new weekholidaystruct[]{
new weekholidaystruct(5, 2, 1, "母亲节" ),
new weekholidaystruct(5, 3, 1, "全国助残日" ),
new weekholidaystruct(6, 3, 1, "父亲节" ),
new weekholidaystruct(9, 3, 3, "国际和平日" ),
new weekholidaystruct(9, 4, 1, "国际聋人节" ),
new weekholidaystruct(10, 1, 2, "国际住房日" ),
new weekholidaystruct(10, 1, 4, "国际减轻自然灾害日" ),
new weekholidaystruct(11, 4, 5, "感恩节" )
};
#endregion
#endregion
#region 构造函数
#region 公历日期初始化
/// <summary>
/// 用一个标准的公历日期来初使化
/// </summary>
public chinesecalendar(datetime dt)
{
int i;
int leap;
int temp;
int offset;
checkdatelimit(dt);
_date = dt.date;
_datetime = dt;
//农历日期计算部分
leap = 0;
temp = 0;
//计算两天的基本差距
timespan ts = _date - chinesecalendar.minday;
offset = ts.days;
for (i = minyear; i <= maxyear; i++)
{
//求当年农历年天数
temp = getchineseyeardays(i);
if (offset - temp < 1)
break ;
else
{
offset = offset - temp;
}
}
_cyear = i;
//计算该年闰哪个月
leap = getchineseleapmonth(_cyear);
//设定当年是否有闰月
if (leap > 0)
{
_cisleapyear = true ;
}
else
{
_cisleapyear = false ;
}
_cisleapmonth = false ;
for (i = 1; i <= 12; i++)
{
//闰月
if ((leap > 0) && (i == leap + 1) && (_cisleapmonth == false ))
{
_cisleapmonth = true ;
i = i - 1;
temp = getchineseleapmonthdays(_cyear); //计算闰月天数
}
else
{
_cisleapmonth = false ;
temp = getchinesemonthdays(_cyear, i); //计算非闰月天数
}
offset = offset - temp;
if (offset <= 0) break ;
}
offset = offset + temp;
_cmonth = i;
_cday = offset;
}
#endregion
#region 农历日期初始化
/// <summary>
/// 用农历的日期来初使化
/// </summary>
/// <param name="cy">农历年</param>
/// <param name="cm">农历月</param>
/// <param name="cd">农历日</param>
/// <param name="leapflag">闰月标志</param>
public chinesecalendar( int cy, int cm, int cd, bool leapmonthflag)
{
int i, leap, temp, offset;
checkchinesedatelimit(cy, cm, cd, leapmonthflag);
_cyear = cy;
_cmonth = cm;
_cday = cd;
offset = 0;
for (i = minyear; i < cy; i++)
{
//求当年农历年天数
temp = getchineseyeardays(i);
offset = offset + temp;
}
//计算该年应该闰哪个月
leap = getchineseleapmonth(cy);
if (leap != 0)
{
this ._cisleapyear = true ;
}
else
{
this ._cisleapyear = false ;
}
if (cm != leap)
{
//当前日期并非闰月
_cisleapmonth = false ;
}
else
{
//使用用户输入的是否闰月月份
_cisleapmonth = leapmonthflag;
}
//当年没有闰月||计算月份小于闰月
if ((_cisleapyear == false ) || (cm < leap))
{
for (i = 1; i < cm; i++)
{
temp = getchinesemonthdays(cy, i); //计算非闰月天数
offset = offset + temp;
}
//检查日期是否大于最大天
if (cd > getchinesemonthdays(cy, cm))
{
throw new exception( "不合法的农历日期" );
}
//加上当月的天数
offset = offset + cd;
}
//是闰年,且计算月份大于或等于闰月
else
{
for (i = 1; i < cm; i++)
{
//计算非闰月天数
temp = getchinesemonthdays(cy, i);
offset = offset + temp;
}
//计算月大于闰月
if (cm > leap)
{
temp = getchineseleapmonthdays(cy); //计算闰月天数
offset = offset + temp; //加上闰月天数
if (cd > getchinesemonthdays(cy, cm))
{
throw new exception( "不合法的农历日期" );
}
offset = offset + cd;
}
//计算月等于闰月
else
{
//如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数
if ( this ._cisleapmonth == true ) //计算月为闰月
{
temp = getchinesemonthdays(cy, cm); //计算非闰月天数
offset = offset + temp;
}
if (cd > getchineseleapmonthdays(cy))
{
throw new exception( "不合法的农历日期" );
}
offset = offset + cd;
}
}
_date = minday.adddays(offset);
}
#endregion
#endregion
#region 私有函数
#region getchinesemonthdays
/// <summary>
/// //传回农历y年m月的总天数
/// </summary>
private int getchinesemonthdays( int year, int month)
{
if (bittest32((lunardatearray[year - minyear] & 0x0000ffff), (16 - month)))
{
return 30;
}
else
{
return 29;
}
}
#endregion
#region getchineseleapmonth
/// <summary>
/// 传回农历 y年闰哪个月 1-12 , 没闰传回 0
/// </summary>
private int getchineseleapmonth( int year)
{
return lunardatearray[year - minyear] & 0xf;
}
#endregion
#region getchineseleapmonthdays
/// <summary>
/// 传回农历y年闰月的天数
/// </summary>
private int getchineseleapmonthdays( int year)
{
if (getchineseleapmonth(year) != 0)
{
if ((lunardatearray[year - minyear] & 0x10000) != 0)
{
return 30;
}
else
{
return 29;
}
}
else
{
return 0;
}
}
#endregion
#region getchineseyeardays
/// <summary>
/// 取农历年一年的天数
/// </summary>
private int getchineseyeardays( int year)
{
int i, f, sumday, info;
sumday = 348; //29天*12个月
i = 0x8000;
info = lunardatearray[year - minyear] & 0x0ffff;
//计算12个月中有多少天为30天
for ( int m = 0; m < 12; m++)
{
f = info & i;
if (f != 0)
{
sumday++;
}
i = i >> 1;
}
return sumday + getchineseleapmonthdays(year);
}
#endregion
#region getchinesehour
/// <summary>
/// 获得当前时间的时辰
/// </summary>
private string getchinesehour(datetime dt)
{
int _hour, _minute, offset, i;
int indexgan;
string ganhour, zhihour;
string tmpgan;
//计算时辰的地支
_hour = dt.hour; //获得当前时间小时
_minute = dt.minute; //获得当前时间分钟
if (_minute != 0) _hour += 1;
offset = _hour / 2;
if (offset >= 12) offset = 0;
//zhihour = zhistr[offset].tostring();
//计算天干
timespan ts = this ._date - ganzhistartday;
i = ts.days % 60;
//ganstr[i % 10] 为日的天干,(n*2-1) %10得出地支对应,n从1开始
indexgan = ((i % 10 + 1) * 2 - 1) % 10 - 1;
tmpgan = ganstr.substring(indexgan) + ganstr.substring(0, indexgan + 2); //凑齐12位
//ganhour = ganstr[((i % 10 + 1) * 2 - 1) % 10 - 1].tostring();
return tmpgan[offset].tostring() + zhistr[offset].tostring();
}
#endregion
#region checkdatelimit
/// <summary>
/// 检查公历日期是否符合要求
/// </summary>
private void checkdatelimit(datetime dt)
{
if ((dt < minday) || (dt > maxday))
{
throw new exception( "超出可转换的日期" );
}
}
#endregion
#region checkchinesedatelimit
/// <summary>
/// 检查农历日期是否合理
/// </summary>
private void checkchinesedatelimit( int year, int month, int day, bool leapmonth)
{
if ((year < minyear) || (year > maxyear))
{
throw new exception( "非法农历日期" );
}
if ((month < 1) || (month > 12))
{
throw new exception( "非法农历日期" );
}
if ((day < 1) || (day > 30)) //中国的月最多30天
{
throw new exception( "非法农历日期" );
}
int leap = getchineseleapmonth(year); // 计算该年应该闰哪个月
if ((leapmonth == true ) && (month != leap))
{
throw new exception( "非法农历日期" );
}
}
#endregion
#region convertnumtochinesenum
/// <summary>
/// 将0-9转成汉字形式
/// </summary>
private string convertnumtochinesenum( char n)
{
if ((n < '0' ) || (n > '9' )) return "" ;
switch (n)
{
case '0' :
return hznum[0].tostring();
case '1' :
return hznum[1].tostring();
case '2' :
return hznum[2].tostring();
case '3' :
return hznum[3].tostring();
case '4' :
return hznum[4].tostring();
case '5' :
return hznum[5].tostring();
case '6' :
return hznum[6].tostring();
case '7' :
return hznum[7].tostring();
case '8' :
return hznum[8].tostring();
case '9' :
return hznum[9].tostring();
default :
return "" ;
}
}
#endregion
#region bittest32
/// <summary>
/// 测试某位是否为真
/// </summary>
private bool bittest32( int num, int bitpostion)
{
if ((bitpostion > 31) || (bitpostion < 0))
throw new exception( "error param: bitpostion[0-31]:" + bitpostion.tostring());
int bit = 1 << bitpostion;
if ((num & bit) == 0)
{
return false ;
}
else
{
return true ;
}
}
#endregion
#region convertdayofweek
/// <summary>
/// 将星期几转成数字表示
/// </summary>
private int convertdayofweek(dayofweek dayofweek)
{
switch (dayofweek)
{
case dayofweek.sunday:
return 1;
case dayofweek.monday:
return 2;
case dayofweek.tuesday:
return 3;
case dayofweek.wednesday:
return 4;
case dayofweek.thursday:
return 5;
case dayofweek.friday:
return 6;
case dayofweek.saturday:
return 7;
default :
return 0;
}
}
#endregion
#region compareweekdayholiday
/// <summary>
/// 比较当天是不是指定的第周几
/// </summary>
private bool compareweekdayholiday(datetime date, int month, int week, int day)
{
bool ret = false ;
if (date.month == month) //月份相同
{
if (convertdayofweek(date.dayofweek) == day) //星期几相同
{
datetime firstday = new datetime(date.year, date.month, 1); //生成当月第一天
int i = convertdayofweek(firstday.dayofweek);
int firweekdays = 7 - convertdayofweek(firstday.dayofweek) + 1; //计算第一周剩余天数
if (i > day)
{
if ((week - 1) * 7 + day + firweekdays == date.day)
{
ret = true ;
}
}
else
{
if (day + firweekdays + (week - 2) * 7 == date.day)
{
ret = true ;
}
}
}
}
return ret;
}
#endregion
#endregion
#region 属性
#region 节日
#region newcalendarholiday
/// <summary>
/// 计算中国农历节日
/// </summary>
public string newcalendarholiday
{
get
{
string tempstr = "" ;
if ( this ._cisleapmonth == false ) //闰月不计算节日
{
foreach (lunarholidaystruct lh in lholidayinfo)
{
if ((lh.month == this ._cmonth) && (lh.day == this ._cday))
{
tempstr = lh.holidayname;
break ;
}
}
//对除夕进行特别处理
if ( this ._cmonth == 12)
{
int i = getchinesemonthdays( this ._cyear, 12); //计算当年农历12月的总天数
if ( this ._cday == i) //如果为最后一天
{
tempstr = "除夕" ;
}
}
}
return tempstr;
}
}
#endregion
#region weekdayholiday
/// <summary>
/// 按某月第几周第几日计算的节日
/// </summary>
public string weekdayholiday
{
get
{
string tempstr = "" ;
foreach (weekholidaystruct wh in wholidayinfo)
{
if (compareweekdayholiday(_date, wh.month, wh.weekatmonth, wh.weekday))
{
tempstr = wh.holidayname;
break ;
}
}
return tempstr;
}
}
#endregion
#region dateholiday
/// <summary>
/// 按公历日计算的节日
/// </summary>
public string dateholiday
{
get
{
string tempstr = "" ;
foreach (solarholidaystruct sh in sholidayinfo)
{
if ((sh.month == _date.month) && (sh.day == _date.day))
{
tempstr = sh.holidayname;
break ;
}
}
return tempstr;
}
}
#endregion
#endregion
#region 公历日期
#region date
/// <summary>
/// 取对应的公历日期
/// </summary>
public datetime date
{
get { return _date; }
set { _date = value; }
}
#endregion
#region weekday
/// <summary>
/// 取星期几
/// </summary>
public dayofweek weekday
{
get { return _date.dayofweek; }
}
#endregion
#region weekdaystr
/// <summary>
/// 周几的字符
/// </summary>
public string weekdaystr
{
get
{
switch (_date.dayofweek)
{
case dayofweek.sunday:
return "星期日" ;
case dayofweek.monday:
return "星期一" ;
case dayofweek.tuesday:
return "星期二" ;
case dayofweek.wednesday:
return "星期三" ;
case dayofweek.thursday:
return "星期四" ;
case dayofweek.friday:
return "星期五" ;
default :
return "星期六" ;
}
}
}
#endregion
#region datestring
/// <summary>
/// 公历日期中文表示法 如一九九七年七月一日
/// </summary>
public string datestring
{
get
{
return "公元" + this ._date.tolongdatestring();
}
}
#endregion
#region isleapyear
/// <summary>
/// 当前是否公历闰年
/// </summary>
public bool isleapyear
{
get
{
return datetime.isleapyear( this ._date.year);
}
}
#endregion
#region chineseconstellation
/// <summary>
/// 28星宿计算
/// </summary>
public string chineseconstellation
{
get
{
int offset = 0;
int modstarday = 0;
timespan ts = this ._date - chineseconstellationreferday;
offset = ts.days;
modstarday = offset % 28;
return (modstarday >= 0 ? _chineseconstellationname[modstarday] : _chineseconstellationname[27 + modstarday]);
}
}
#endregion
#region chinesehour
/// <summary>
/// 时辰
/// </summary>
public string chinesehour
{
get
{
return getchinesehour(_datetime);
}
}
#endregion
#endregion
#region 农历日期
#region ischineseleapmonth
/// <summary>
/// 是否闰月
/// </summary>
public bool ischineseleapmonth
{
get { return this ._cisleapmonth; }
}
#endregion
#region ischineseleapyear
/// <summary>
/// 当年是否有闰月
/// </summary>
public bool ischineseleapyear
{
get
{
return this ._cisleapyear;
}
}
#endregion
#region chineseday
/// <summary>
/// 农历日
/// </summary>
public int chineseday
{
get { return this ._cday; }
}
#endregion
#region chinesedaystring
/// <summary>
/// 农历日中文表示
/// </summary>
public string chinesedaystring
{
get
{
switch ( this ._cday)
{
case 0:
return "" ;
case 10:
return "初十" ;
case 20:
return "二十" ;
case 30:
return "三十" ;
default :
return nstr2[( int )(_cday / 10)].tostring() + nstr1[_cday % 10].tostring();
}
}
}
#endregion
#region chinesemonth
/// <summary>
/// 农历的月份
/// </summary>
public int chinesemonth
{
get { return this ._cmonth; }
}
#endregion
#region chinesemonthstring
/// <summary>
/// 农历月份字符串
/// </summary>
public string chinesemonthstring
{
get
{
return _monthstring[ this ._cmonth];
}
}
#endregion
#region chineseyear
/// <summary>
/// 取农历年份
/// </summary>
public int chineseyear
{
get { return this ._cyear; }
}
#endregion
#region chineseyearstring
/// <summary>
/// 取农历年字符串如,一九九七年
/// </summary>
public string chineseyearstring
{
get
{
string tempstr = "" ;
string num = this ._cyear.tostring();
for ( int i = 0; i < 4; i++)
{
tempstr += convertnumtochinesenum(num[i]);
}
return tempstr + "年" ;
}
}
#endregion
#region chinesedatestring
/// <summary>
/// 取农历日期表示法:农历一九九七年正月初五
/// </summary>
public string chinesedatestring
{
get
{
if ( this ._cisleapmonth == true )
{
return "农历" + chineseyearstring + "闰" + chinesemonthstring + chinesedaystring;
}
else
{
return "农历" + chineseyearstring + chinesemonthstring + chinesedaystring;
}
}
}
#endregion
#region chinesetwentyfourday
/// <summary>
/// 定气法计算二十四节气,二十四节气是按地球公转来计算的,并非是阴历计算的
/// </summary>
/// <remarks>
/// 节气的定法有两种。古代历法采用的称为"恒气",即按时间把一年等分为24份,
/// 每一节气平均得15天有余,所以又称"平气"。现代农历采用的称为"定气",即
/// 按地球在轨道上的位置为标准,一周360°,两节气之间相隔15°。由于冬至时地
/// 球位于近日点附近,运动速度较快,因而太阳在黄道上移动15°的时间不到15天。
/// 夏至前后的情况正好相反,太阳在黄道上移动较慢,一个节气达16天之多。采用
/// 定气时可以保证春、秋两分必然在昼夜平分的那两天。
/// </remarks>
public string chinesetwentyfourday
{
get
{
datetime basedateandtime = new datetime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 am#
datetime newdate;
double num;
int y;
string tempstr = "" ;
y = this ._date.year;
for ( int i = 1; i <= 24; i++)
{
num = 525948.76 * (y - 1900) + sterminfo[i - 1];
newdate = basedateandtime.addminutes(num); //按分钟计算
if (newdate.dayofyear == _date.dayofyear)
{
tempstr = solarterm[i - 1];
break ;
}
}
return tempstr;
}
}
//当前日期前一个最近节气
public string chinesetwentyfourprevday
{
get
{
datetime basedateandtime = new datetime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 am#
datetime newdate;
double num;
int y;
string tempstr = "" ;
y = this ._date.year;
for ( int i = 24; i >= 1; i--)
{
num = 525948.76 * (y - 1900) + sterminfo[i - 1];
newdate = basedateandtime.addminutes(num); //按分钟计算
if (newdate.dayofyear < _date.dayofyear)
{
tempstr = string .format( "{0}[{1}]" , solarterm[i - 1], newdate.tostring( "yyyy-mm-dd" ));
break ;
}
}
return tempstr;
}
}
//当前日期后一个最近节气
public string chinesetwentyfournextday
{
get
{
datetime basedateandtime = new datetime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 am#
datetime newdate;
double num;
int y;
string tempstr = "" ;
y = this ._date.year;
for ( int i = 1; i <= 24; i++)
{
num = 525948.76 * (y - 1900) + sterminfo[i - 1];
newdate = basedateandtime.addminutes(num); //按分钟计算
if (newdate.dayofyear > _date.dayofyear)
{
tempstr = string .format( "{0}[{1}]" , solarterm[i - 1], newdate.tostring( "yyyy-mm-dd" ));
break ;
}
}
return tempstr;
}
}
#endregion
#endregion
#region 星座
/// <summary>
/// 计算指定日期的星座序号
/// </summary>
public string constellation
{
get
{
int index = 0;
int y, m, d;
y = _date.year;
m = _date.month;
d = _date.day;
y = m * 100 + d;
if (((y >= 321) && (y <= 419))) { index = 0; }
else if ((y >= 420) && (y <= 520)) { index = 1; }
else if ((y >= 521) && (y <= 620)) { index = 2; }
else if ((y >= 621) && (y <= 722)) { index = 3; }
else if ((y >= 723) && (y <= 822)) { index = 4; }
else if ((y >= 823) && (y <= 922)) { index = 5; }
else if ((y >= 923) && (y <= 1022)) { index = 6; }
else if ((y >= 1023) && (y <= 1121)) { index = 7; }
else if ((y >= 1122) && (y <= 1221)) { index = 8; }
else if ((y >= 1222) || (y <= 119)) { index = 9; }
else if ((y >= 120) && (y <= 218)) { index = 10; }
else if ((y >= 219) && (y <= 320)) { index = 11; }
else { index = 0; }
return _constellationname[index];
}
}
#endregion
#region 属相
#region animal
/// <summary>
/// 计算属相的索引,注意虽然属相是以农历年来区别的,但是目前在实际使用中是按公历来计算的
/// 鼠年为1,其它类推
/// </summary>
public int animal
{
get
{
int offset = _date.year - animalstartyear;
return (offset % 12) + 1;
}
}
#endregion
#region animalstring
/// <summary>
/// 取属相字符串
/// </summary>
public string animalstring
{
get
{
int offset = _date.year - animalstartyear; //阳历计算
//int offset = this._cyear - animalstartyear; 农历计算
return animalstr[offset % 12].tostring();
}
}
#endregion
#endregion
#region 天干地支
#region ganzhiyearstring
/// <summary>
/// 取农历年的干支表示法如 乙丑年
/// </summary>
public string ganzhiyearstring
{
get
{
string tempstr;
int i = ( this ._cyear - ganzhistartyear) % 60; //计算干支
tempstr = ganstr[i % 10].tostring() + zhistr[i % 12].tostring() + "年" ;
return tempstr;
}
}
#endregion
#region ganzhimonthstring
/// <summary>
/// 取干支的月表示字符串,注意农历的闰月不记干支
/// </summary>
public string ganzhimonthstring
{
get
{
//每个月的地支总是固定的,而且总是从寅月开始
int zhiindex;
string zhi;
if ( this ._cmonth > 10)
{
zhiindex = this ._cmonth - 10;
}
else
{
zhiindex = this ._cmonth + 2;
}
zhi = zhistr[zhiindex - 1].tostring();
//根据当年的干支年的干来计算月干的第一个
int ganindex = 1;
string gan;
int i = ( this ._cyear - ganzhistartyear) % 60; //计算干支
switch (i % 10)
{
#region ...
case 0: //甲
ganindex = 3;
break ;
case 1: //乙
ganindex = 5;
break ;
case 2: //丙
ganindex = 7;
break ;
case 3: //丁
ganindex = 9;
break ;
case 4: //戊
ganindex = 1;
break ;
case 5: //己
ganindex = 3;
break ;
case 6: //庚
ganindex = 5;
break ;
case 7: //辛
ganindex = 7;
break ;
case 8: //壬
ganindex = 9;
break ;
case 9: //癸
ganindex = 1;
break ;
#endregion
}
gan = ganstr[(ganindex + this ._cmonth - 2) % 10].tostring();
return gan + zhi + "月" ;
}
}
#endregion
#region ganzhidaystring
/// <summary>
/// 取干支日表示法
/// </summary>
public string ganzhidaystring
{
get
{
int i, offset;
timespan ts = this ._date - ganzhistartday;
offset = ts.days;
i = offset % 60;
return ganstr[i % 10].tostring() + zhistr[i % 12].tostring() + "日" ;
}
}
#endregion
#region ganzhidatestring
/// <summary>
/// 取当前日期的干支表示法如 甲子年乙丑月丙庚日
/// </summary>
public string ganzhidatestring
{
get
{
return ganzhiyearstring + ganzhimonthstring + ganzhidaystring;
}
}
#endregion
#endregion
#endregion
}
}
|
效果格式如下:
以上所述是小编给大家介绍的c# 日历类功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/Liyuting/p/7059610.html