Csharp: 阴历年甲子干支算法錯誤問題

复制代码
 1 /// <summary>
 2         /// 农历日历
 3         /// </summary>
 4         private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
 5         /// <summary>
 6         /// 中文数字
 7         /// </summary>
 8         private static string ChineseNumber = "〇一二三四五六七八九";
 9         /// <summary>
10         /// 甲子
11         /// </summary>
12         public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
13         /// <summary>
14         /// 阴历年干支
15         /// </summary>
16         public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
17         /// <summary>
18         /// 属相
19         /// </summary>
20         public const string ShuXiang = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
21 
22         /// <summary>
23         /// 甲子记年法
24         /// </summary>
25         /// <param name="time"></param>
26         /// <returns></returns>
27         public static string GetStemBranch(DateTime time)
28         {
29             string stemBranch = string.Empty;
30             int sexagenaryYear = calendar.GetSexagenaryYear(time);//与指定日期对应的甲子(60 年)循环中的年
31             if (sexagenaryYear % 10 == 0 && sexagenaryYear % 12 == 0)
32             {
33                  stemBranch = CelestialStem.Substring(sexagenaryYear % 10+9, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 +11, 1); 
34             }
35             else if (sexagenaryYear % 10 != 0 && sexagenaryYear % 12 == 0)
36             {
37                 stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 +11, 1);
38             }
39             else if (sexagenaryYear % 10 == 0 && sexagenaryYear % 12 != 0)
40             {
41                 stemBranch = CelestialStem.Substring(sexagenaryYear % 10+9, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 -1, 1);
42             }
43             else
44             {
45                 stemBranch = CelestialStem.Substring(sexagenaryYear % 10 - 1, 1) + TerrestrialBranch.Substring(sexagenaryYear % 12 - 1, 1);
46             }
47             return stemBranch;
48         }
49         /// <summary>
50         /// 生肖
51         /// </summary>
52         /// <param name="time"></param>
53         /// <returns></returns>
54         public static string GetSX(DateTime time)
55         {
56             string SX = string.Empty;
57             int sexagenaryYear = calendar.GetSexagenaryYear(time);//与指定日期对应的甲子(60 年)循环中的年
58             if (sexagenaryYear % 12 == 0)
59             {
60                 SX = ShuXiang.Substring(sexagenaryYear % 12 + 11, 1);
61             }
62             else
63             {
64                 SX = ShuXiang.Substring(sexagenaryYear % 12 - 1, 1);
65             }
66             return SX;
67         }
68         /// <summary>
69         /// 
70         /// </summary>
71         public GZForm()
72         {
73             InitializeComponent();
74         }
75         /// <summary>
76         /// 
77         /// </summary>
78         /// <param name="sender"></param>
79         /// <param name="e"></param>
80         private void GZForm_Load(object sender, EventArgs e)
81         {
82             this.label1.Text = "农历今天是:" + GetStemBranch(DateTime.Now) + "";
83             this.label2.Text = "农历今生肖是:" + GetSX(DateTime.Now);
84         }
85         /// <summary>
86         /// 
87         /// </summary>
88         /// <param name="sender"></param>
89         /// <param name="e"></param>
90         private void button1_Click(object sender, EventArgs e)
91         {
92             this.label1.Text = "农历是:" + GetStemBranch(this.dateTimePicker1.Value) + "";
93             this.label2.Text = "生肖是:" + GetSX(this.dateTimePicker1.Value);
94         }
复制代码

 

posted @   ®Geovin Du Dream Park™  阅读(666)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2009-02-18 C# 2.0 Graphics 画雪人
< 2013年2月 >
27 28 29 30 31 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 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示