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; }
        }
    }
}
以下是C#获取身份证号男女的代码示例: ```csharp // 获取身份证中的性别 public static bool GetGenderByIdCard(string idCard) { if (string.IsNullOrWhiteSpace(idCard)) { return false; } return Convert.ToBoolean(int.Parse(idCard.Substring(16, 1)) % 2); } // 示例用法 string idCard = "11010119900307771X"; bool isMale = GetGenderByIdCard(idCard); Console.WriteLine(isMale ? "男性" : "女性"); // 输出:男性 ``` 上述代码中,`GetGenderByIdCard`方法接收一个身份证号码作为参数,通过解析身份证号码中的第17位数字来判断性别,如果是奇数则为男性,偶数则为女性。 另外,如果需要获取身份证中的年龄,可以使用下面的代码: ```csharp // 获取身份证中的年龄 public static int GetAgeByIdCard(string idCard) { int age = 0; if (!string.IsNullOrWhiteSpace(idCard)) { var subStr = string.Empty; if (idCard.Length == 18) { subStr = idCard.Substring(6, 8).Insert(4, "-").Insert(7, "-"); } else if (idCard.Length == 15) { subStr = ("19" + idCard.Substring(6, 6)).Insert(4, "-").Insert(7, "-"); } TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(subStr)); age = ts.Days / 365; } return age; } // 示例用法 string idCard = "11010119900307771X"; int age = GetAgeByIdCard(idCard); Console.WriteLine($"年龄为:{age}岁"); // 输出:年龄为:31岁 ``` 上述代码中,`GetAgeByIdCard`方法接收一个身份证号码作为参数,通过解析身份证号码中的出生日期来计算年龄
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值