C#解析身份证号

public class IDCard
    {
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get; set; }
        /// <summary>
        /// 性别编码  1 男   2 女
        /// </summary>
        public string SexCode { get; set; }
        /// <summary>
        /// 生日
        /// </summary>
        public string BirthDay { get; set; }
        /// <summary>
        /// 生肖
        /// </summary>
        public string ShengXiao { get; set; }
        /// <summary>
        /// 星座
        /// </summary>
        public string XingZuo { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int? Age { get; set; }
    }
public IDCard DecodeIDCard(string keys)
        {
            IDCard card = new IDCard();
            if (keys != null && CheckChinaIDCardNumberFormat(keys))
            {
                int sex = int.Parse(keys.Substring(16, 1));
                if (sex % 2 == 0)
                {
                    card.Sex = "";
                    card.SexCode = "2";
                }
                else
                {
                    card.Sex = "";
                    card.SexCode = "1";
                }
                card.BirthDay = keys.Substring(6, 4) + "-" + keys.Substring(10, 2) + "-" + keys.Substring(12, 2);
                card.ShengXiao = sx(keys.Substring(6, 4));
                card.XingZuo = xz(keys.Substring(10, 2), keys.Substring(12, 2));
                card.Age = CalculateAge(card.BirthDay);
            }
            
            return card;
        }
        /// <summary>
        /// 获取生肖
        /// </summary>
        /// <param name="y"></param>
        /// <returns></returns>
        public string sx(string y)
        {
            uint temp_y = uint.Parse(y);
            temp_y = (temp_y - 1900) % 12;
            string result = "";
            switch (temp_y)
            {
                case 0:
                    result = "";
                    break;
                case 1:
                    result = "";
                    break;
                case 2:
                    result = "";
                    break;
                case 3:
                    result = "";
                    break;
                case 4:
                    result = "";
                    break;
                case 5:
                    result = "";
                    break;
                case 6:
                    result = "";
                    break;
                case 7:
                    result = "";
                    break;
                case 8:
                    result = "";
                    break;
                case 9:
                    result = "";
                    break;
                case 10:
                    result = "";
                    break;
                case 11:
                    result = "";
                    break;
                default:
                    result = "not found";
                    break;
            }
            //鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
            return result;
        }
        /// <summary>
        /// 获取星座
        /// </summary>
        /// <param name="mon"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public string xz(string mon, string day)
        {
            if (mon == "01")
            {
                if (int.Parse(day) <= 19)
                    return "水瓶";
                else
                    return "水瓶";
            }

            else if (mon == "02")
            {
                if (int.Parse(day) <= 19)
                    return "水瓶";
                else
                    return "双鱼";
            }
            else if (mon == "03")
            {
                if (int.Parse(day) <= 20)
                    return "双鱼";
                else
                    return "白羊";
            }
            else if (mon == "04")
            {
                if (int.Parse(day) <= 20)
                    return "白羊";
                else
                    return "金牛";
            }
            else if (mon == "05")
            {
                if (int.Parse(day) <= 20)
                    return "金牛";
                else
                    return "双子";
            }
            else if (mon == "06")
            {
                if (int.Parse(day) <= 21)
                    return "双子";
                else
                    return "巨蟹";
            }
            else if (mon == "07")
            {
                if (int.Parse(day) <= 22)
                    return "巨蟹";
                else
                    return "狮子";
            }
            else if (mon == "08")
            {
                if (int.Parse(day) <= 22)
                    return "狮子";
                else
                    return "处女";
            }
            else if (mon == "09")
            {
                if (int.Parse(day) <= 22)
                    return "处女";
                else
                    return "天平";
            }
            else if (mon == "10")
            {
                if (int.Parse(day) <= 22)
                    return "天平";
                else
                    return "天蝎";
            }
            else if (mon == "11")
            {
                if (int.Parse(day) <= 21)
                    return "天蝎";
                else
                    return "射手";
            }
            else
            {
                if (int.Parse(day) <= 21)
                    return "射手";
                else
                    return "魔羯";
            }
        }
        /// <summary>
        /// 根据出生日期,计算精确的年龄
        /// </summary>
        /// <param name="birthDate">生日</param>
        /// <returns></returns>
        public static int CalculateAge(string birthDay)
        {
            DateTime birthDate = DateTime.Parse(birthDay);
            DateTime nowDateTime = DateTime.Now;
            int age = nowDateTime.Year - birthDate.Year;
            //再考虑月、天的因素
            if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
            {
                age--;
            }
            return age;
        }

IDCard card=DecodeIDCard("身份证号")

 

posted @ 2022-07-28 18:39  Smile丶品位  阅读(220)  评论(1编辑  收藏  举报