分享一個自己寫的取中國農曆相關資料的類。包含:農曆年月日,生肖,星座,年齡,天干,地支等方法

發表於2019-04-06

分享一個自己寫的取中國農曆相關資料的類。包含:農曆年月日,生肖,星座,年齡,天干,地支等方法。此類自己花了一上午的時間寫的,適用於像相親網等類似的網站

主要使用了微軟針對東亞地區的農曆類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 DateTime GetLunarDate()
        {
            return new DateTime(this.GetLunarYear(), this.GetLunarMonth(), this.GetLunarDay());
        }

        /// <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();
        }

  

 

呼叫結果,直接看圖吧:

相關文章