Unity UGUI自制日历控件 带农历日期

新手小白无聊时做的一个 日历控件,需要的朋友可以借鉴下,高手大神也可以指点一下。

核心代码

  1. 获取某年某月有多少天
    /// 获取某年某月有多少天
    /// </summary>
    /// <param name="_year">年份</param>
    /// <param name="_month">月份</param>
    /// <returns></returns>
    public int DaysFromMonth(int _year,int _month)
    { 
        return DateTime.DaysInMonth(_year, _month);
    }
  1. 根据年份和月份,获取当月的1号是星期几
    // 根据年月返回月份1号的星期
    DayOfWeek WeekFromDay(int _year, int _month)
    { 
        return DateTime.Parse(_year + "/" + _month + "/" + 1).DayOfWeek;
    }
  1. 关于农历日期的获取
    天干地支纪年法,将十天干和十二地支安顺序打牌组合成干支,用于纪年。按此排法,当10天干排了六轮,12地支排了五轮之后,可构成60干支。继续拍下去又恢复原状,周而复始,即如民间所说的“六十年转甲子”。
    10天干,12地支,12生肖
    // 十天干
    private static string[] heavenlyStems  = {  "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
 
    // 十二地支
    private static string[] earthlyBranches = {  "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
 
    // 十二生肖
    private static string[] zodiacs = {  "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

<1> 获取农历的年份

   /// <summary>
   /// 获取农历的年份
   /// </summary>
   /// <param name="_year"></param>
   /// <returns></returns>
   public string GetLunarYear(int _year)
   { 
       if (_year > 3)
       { 
           int hs = (_year - 4) % 10;
           int eb = (_year - 4) % 12;
 
           return string.Concat(heavenlyStems[hs], earthlyBranches[eb], "[", zodiacs[eb], "]年");
       }
       else
       { 
           Debug.Log("----------------- 无效的年份 ----------------");
           return "";
       }
   }

<2> 获取农历的月份

    /// <summary>
    /// 获取农历的月份
    /// </summary>
    /// <param name="_month"></param>
    /// <returns></returns>
    public string GetLunarMonth(int _month)
    { 
        switch (_month)
        { 
            case 1:
                return "正月";
            case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
                return string.Concat(DayToString(_month), "月");
            case 11:
                return "十一月";
            case 12:
                return "十二(腊)月";
        }
 
        return "";
    }

<3> 获取农历的日

    /// <summary>
    /// 获取农历的天
    /// </summary>
    /// <param name="_day"></param>
    /// <returns></returns>
    public string GetLunarDay(int _day)
    { 
        if (_day < 11)
            return "初" + DayToString(_day);
        else if (_day > 10 && _day < 21)
            return "十" + DayToString(_day % 10);
        else if (_day > 20 && _day < 30)
            return "廿" + DayToString(_day % 20);
        else
            return "三十" + DayToString(_day % 30);
    }
  1. 公历的日转农历
string DayToString(int _index)
    { 
        switch (_index)
        { 
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            case 7:
                return "七";
            case 8:
                return "八";
            case 9:
                return "九";
            case 10:
                return "十";
        }
 
        return "";
    }
  1. 显示农历的日期:
    /// <summary>
    /// 显示农历日期
    /// </summary>
    /// <param name="_day"></param>
    void ShowLunarDateTime()
    { 
        txtLunarDateTime.text = string.Concat(GetLunarYear(year), " ", GetLunarMonth(month), " ", GetLunarDay(day));
    }
    原文作者:梦丶初寒
    原文地址: https://blog.csdn.net/Glow0129/article/details/102544063
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞