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 = "32128119930718125X";//隨便拼的,如有雷同,純屬搞怪哈
            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
            {
                if (identityCard.Length != 15 && identityCard.Length != 18)//身份證號碼只能為15位或18位其它不合法
                {
                    return null;
                }
            }

            BirthdayAgeSex entity = new BirthdayAgeSex();
            string strSex = string.Empty;
            if (identityCard.Length == 18)//處理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; }
        }
    }
}

(ps:多年前寫的了,今天看了下,確實很水啊。。。。)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM