yangle
2022-10-08 b27cc0c7af4cb9f5b404c95f90c63be22668d0a6
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
 
namespace BLL
{
    public class ClsDateToNongli
    {
        /// <summary>   
        /// 实例化一个  ChineseLunisolarCalendar       
        /// </summary>  
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
        /// <summary>  
        /// 十天干  
        /// </summary>  
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        /// <summary>  
        ///  十二地支  
        ///  </summary>  
        private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        /// <summary>  
        /// 十二生肖  
        /// </summary>  
        private static string[] sx = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
        /// <summary>  
        ///  返回农历天干地支年  
        ///   </summary>  
        ///    <param name="year">农历年</param>  
        ///    <returns></returns>  
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;
                return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
            }
            throw new ArgumentOutOfRangeException("无效的年份!");
        }
        /// <summary>  
        /// 农历月  
        /// </summary>  
        /// <returns></returns>  
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
        /// <summary>  
        /// 农历日  
        /// </summary>  
        private static string[] days1 = { "初", "十", "廿", "三" };
        /// <summary>  
        ///  农历日  
        /// </summary>  
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        /// <summary>  
        /// 返回农历月  
        /// </summary>  
        /// <param name="month">月份</param>  
        /// <returns></returns>  
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }
            throw new ArgumentOutOfRangeException("无效的月份!");
        }
        /// <summary>  
        /// 返回农历日  
        /// </summary>  
        /// <param name="day">天</param>  
        /// <returns></returns>  
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }
            throw new ArgumentOutOfRangeException("无效的日!");
        }
 
        /// <summary>   
        /// 根据公历获取农历日期  
        /// </summary>  
        /// <param name="datetime">公历日期</param>  
        /// <returns></returns>  
        public static string GetChineseDateTime(DateTime datetime)
        {
            //农历的年月日  
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //获取闰月, 0 则表示没有闰月   
            int leapMonth = ChineseCalendar.GetLeapMonth(year);
            bool isleap = false;
            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {
                    //闰月       
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }
            return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }  
    }
}