其他分享
首页 > 其他分享> > 分享一个自己写的取中国农历相关数据的类。包含:农历年月日,生肖,星座,年龄,天干,地支等方法

分享一个自己写的取中国农历相关数据的类。包含:农历年月日,生肖,星座,年龄,天干,地支等方法

作者:互联网

分享一个自己写的取中国农历相关数据的类。包含:农历年月日,生肖,星座,年龄,天干,地支等方法。此类自己花了一上午的时间写的,适用于像相亲网等类似的网站

主要使用了微软针对东亚地区的农历类ChineseLunisolarCalendar

这些东西,网上其实有很多,我也参考了一下,但大多写的感觉拼凑的份儿多。也没有统一的这种规范类。

今天自己用到了,就顺便分享给大家,需要的人可以自行收藏。

贴代码吧:

    /// <summary>
    /// 中国农历
    /// </summary>
    public class ChineseCalendar
    {
        /// <summary>
        /// 当前公历日期
        /// </summary>
        public DateTime DateTime { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ChineseCalendar() : this(DateTime.Now) { }

        private readonly ChineseLunisolarCalendar _chinseCaleander;
        private readonly int _sexagenaryYear;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dateTime"></param>
        public ChineseCalendar(DateTime dateTime)
        {
            this.DateTime = dateTime;
            _chinseCaleander = new ChineseLunisolarCalendar();
            _sexagenaryYear = this._chinseCaleander.GetSexagenaryYear(this.DateTime) - 1;
        }

        /// <summary>
        /// 取天干
        /// </summary>
        /// <returns></returns>
        public virtual string GetTiangan()
        {
            var tiangans = new string[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
            return tiangans[_sexagenaryYear % 10];
        }

        /// <summary>
        /// 取地支
        /// </summary>
        /// <returns></returns>
        public virtual string GetDizhi()
        {
            var dizhis = new string[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
            return dizhis[_sexagenaryYear % 12];
        }

        /// <summary>
        /// 取农历年
        /// </summary>
        /// <returns></returns>
        public virtual int GetLunarYear()
        {
            return this._chinseCaleander.GetYear(this.DateTime);
        }

        /// <summary>
        /// 取农历月
        /// </summary>
        /// <returns></returns>
        public virtual int GetLunarMonth()
        {
            return this._chinseCaleander.GetMonth(this.DateTime);
        }

        /// <summary>
        /// 取农历日
        /// </summary>
        /// <returns></returns>
        public virtual int GetLunarDay()
        {
            return this._chinseCaleander.GetDayOfMonth(this.DateTime);
        }

        /// <summary>
        /// 取农历月的规范化描述
        /// </summary>
        /// <returns></returns>
        public virtual string GetLunarMonthString()
        {
            int month = this.GetLunarMonth();
            var des = "";
            var names = new string[] { "*", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" };
            //农历月
            if (month < 1)
                des += "闰" + names[-1 * month].ToString() + "月";
            else
                des += names[month].ToString() + "月";

            return des;
        }

        /// <summary>
        /// 取农历日的规范化描述
        /// </summary>
        /// <returns></returns>
        public virtual string GetLunarDayString()
        {
            var names = new string[]{
                "*",
            "初一","初二","初三","初四","初五",
            "初六","初七","初八","初九","初十",
            "十一","十二","十三","十四","十五",
            "十六","十七","十八","十九","二十",
            "廿一","廿二","廿三","廿四","廿五",
            "廿六","廿七","廿八","廿九","三十"};

            int day = this.GetLunarDay();
            return names[day].ToString() + "日";
        }

        /// <summary>
        /// 取农历月日的规范化描述
        /// </summary>
        /// <returns></returns>
        public virtual string GetLunarMonthAndDayString()
        {
            return this.GetLunarMonthString() + this.GetLunarDayString();
        }

        /// <summary>
        /// 取中国农历年月日的规范化描述
        /// </summary>
        /// <returns></returns>
        public virtual string GetLunarChineseCalendar()
        {
            return string.Join(" ", this.GetZodiac() + "年", this.GetTiangan() + this.GetDizhi(), this.GetLunarMonthAndDayString());
        }

        /// <summary>
        /// 取生肖
        /// </summary>
        /// <returns></returns>
        public virtual string GetZodiac()
        {
            var zodiacs = new string[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
            return zodiacs[_sexagenaryYear % 12];
        }

        /// <summary>
        /// 取星座
        /// </summary>
        /// <returns></returns>
        public virtual string GetAtom()
        {
            var birthday = this.DateTime;
            float birthdayF = 0.00F;

            if (birthday.Month == 1 && birthday.Day < 20)
            {
                birthdayF = float.Parse(string.Format("13.{0}", birthday.Day.ToString("00")));
            }
            else
            {
                birthdayF = float.Parse(string.Format("{0}.{1}", birthday.Month, birthday.Day.ToString("00")));
            }

            //十二星座对应日期
            var atomBound = new float[] { 1.20F, 2.20F, 3.21F, 4.21F, 5.21F, 6.22F, 7.23F, 8.23F, 9.23F, 10.23F, 11.21F, 12.22F, 13.20F };
            //十二星座
            var atoms = new string[] { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

            var atom = "";
            for (int i = 0; i < atomBound.Length - 1; i++)
            {
                if (atomBound[i] <= birthdayF && atomBound[i + 1] > birthdayF)
                {
                    atom = atoms[i];
                    break;
                }
            }

            return atom;
        }

        /// <summary>
        /// 取年龄
        /// </summary>
        /// <returns></returns>
        public virtual int GetAge()
        {
            int now = int.Parse(DateTime.Today.ToString("yyyyMMdd"));

            int dob = int.Parse(this.DateTime.ToString("yyyyMMdd"));

            string dif = (now - dob).ToString();

            string age = "0";

            if (dif.Length > 4)
                age = dif.Substring(0, dif.Length - 4);

            return Convert.ToInt32(age);
        }
    }

  

 

调用方式:

        [Fact]
        public void Test1()
        {
            ChineseCalendar calendar = new ChineseCalendar(Convert.ToDateTime("2019-2-4"));
            var age = calendar.GetAge();
            var atom = calendar.GetAtom();
            var zodiac = calendar.GetZodiac();
            var dizhi = calendar.GetDizhi();
            var lunarChineseCalendar = calendar.GetLunarChineseCalendar();
            var lunarDay = calendar.GetLunarDay();
            var lunarDayString = calendar.GetLunarDayString();
            var lunarMonth = calendar.GetLunarMonth();
            var lunarMonthAndDayString = calendar.GetLunarMonthAndDayString();
            var lunarMonthString = calendar.GetLunarMonthString();
            var lunarYear = calendar.GetLunarYear();
            var tiangan = calendar.GetTiangan();
        }

  

 

调用结果,直接看图吧:

标签:return,string,生肖,DateTime,农历,calendar,var,年月日,public
来源: https://www.cnblogs.com/mazhiyuan/p/10661053.html