c# 身份證獲取出生日期年齡和性別


if (Identification.Length == 18)//處理18位的身份證號碼從號碼中得到生日和性別代碼
{
  string age = Identification.Substring(6, 4) + "-" + Identification.Substring(10, 2) + "-" + Identification.Substring(12, 2);//獲取生辰八字
  string gender = Identification.Substring(14, 3);//獲取性別那個數
  DateTime birthDate = DateTime.Parse(age);
  DateTime nowDateTime = DateTime.Now;
  int age1 = nowDateTime.Year - birthDate.Year;
  //再考慮月、天的因素
  if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
  {
    age1--;
  }
  var Age = age1;
  string Gender = "";
  if (int.Parse(gender) % 2 == 0)//性別代碼為偶數是女性奇數為男性
  {
    Gender = "女";
    //var Setting = db.Setting.Where(p => p.SType == Model.Enums.SettingEnum.age.ToString() && p.SName == "MaxFemale").FirstOrDefault();
    if (Age > Convert.ToInt32(50))
    {
      ModelState.AddModelError("Identification", "年齡大於" + 50);
    }
  }
  else
  {
    Gender = "男";
    //var Setting = db.Setting.Where(p => p.SType == Model.Enums.SettingEnum.age.ToString() && p.SName == "MaxMale").FirstOrDefault();
    if (Age > Convert.ToInt32(55))
    {
      ModelState.AddModelError("Identification", "年齡大於" + 55);
    }
  }
}


免責聲明!

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



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