C#根据身份证号码计算年龄和性别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace calculateAgeBirthdatSexDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string identityCard = "51238119989718125X";//随机拼写
            BirthdayAgeSex entity = new BirthdayAgeSex();
            entity=GetBirthdayAgeSex(identityCard);
            if (entity != null)
            {
                Console.WriteLine(entity.Birthday + "-----" + entity.Sex + "-----" + entity.Age);
            }
            Console.ReadLine();
        }

        public static BirthdayAgeSex GetBirthdayAgeSex(string identityCard)
        {
            if (string.IsNullOrEmpty(identityCard))
            {
                return null;
            }
            else
            {
                //身份证长度15位一代身份证,18位位二代;其他不合法
                if (identityCard.Length != 15 && identityCard.Length != 18)
                {
                    return null;
                }
            }

            BirthdayAgeSex entity = new BirthdayAgeSex();
            string strSex = string.Empty;
            
            //处理18位的身份证号码从号码中得到生日和性别代码
            if (identityCard.Length == 18)
            {
                entity.Birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
                strSex = identityCard.Substring(14, 3);
            }
            if (identityCard.Length == 15)
            {
                entity.Birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
                strSex = identityCard.Substring(12, 3);
            }
            
            entity.Age = CalculateAge(entity.Birthday);//根据生日计算年龄
            if (int.Parse(strSex) % 2 == 0)//性别代码为偶数是女性奇数为男性
            {
                entity.Sex = "女";
            }
            else
            {
                entity.Sex = "男";
            }
            return entity;
        }

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

        /// <summary>
        /// 定义 生日年龄性别 实体
        /// </summary>
        public class BirthdayAgeSex
        {
            public string Birthday { get; set; }
            public int Age { get; set; }
            public string Sex { get; set; }
        }
    }
}

版权声明:本文为CSDN博主「别让青春虚度年华」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45244974/article/details/113995545

 
posted @ 2022-05-04 21:59  86727515  阅读(1069)  评论(0编辑  收藏  举报