c# 身份证获取出生日期年龄和性别

时间:2020-09-17 17:11:56   收藏:0   阅读:48

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!