C# 身份证号取年龄



 #region  身份证验证 

 

      static int[] weight = new int[] { 7, 9,10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; 

      static char[] vCode = new char[] { '1','0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; 

      string address ="11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; 

      int age = 0; 

      bool valid; 

 

 

      public bool Reg_IdentityCard(stringnum) 

      { 

          if (num.Length == 18) 

          { 

              if(!CheckValidCode(num.Substring(0, 17), num[17])) 

                  valid = false; 

              else if(!CheckACode(num.Substring(0, 2))) 

                  valid = false; 

              else if (!GetAge(num.Substring(6,4))) 

                  valid = false; 

              else 

                  valid = true; 

              if (valid) 

              { 

                  int g =Convert.ToInt32(num.Substring(14, 3)); 

                  if (g % 2 == 0) 

                     this.txtSex.Text = ""; 

                  else 

                     this.txtSex.Text = ""; 

                  this.txtAge.Text =age.ToString(); 

                  return true; 

              } 

              else 

              { 

                 MessageBox.Show("身份证号不正确","系统提示", MessageBoxButtons.OK,MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); 

                  return false; 

              } 

          } 

          else if (num.Length == 15) 

          { 

              if (!CheckACode(num.Substring(0,2))) 

                  valid = false; 

              else if (!GetAge("19" +num.Substring(6, 2))) 

                  valid = false; 

              else 

                  valid = true; 

              if (valid) 

              { 

                  int g =Convert.ToInt32(num.Substring(12, 3)); 

                  if (g % 2 == 0) 

                     this.txtSex.Text = ""; 

                  else 

                     this.txtSex.Text = ""; 

                  this.txtAge.Text =age.ToString(); 

                  return true; 

              } 

              else 

              { 

                 MessageBox.Show("身份证号不正确","系统提示", MessageBoxButtons.OK,MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); 

                  return false; 

              } 

          } 

          else 

          { 

             MessageBox.Show("身份证号不正确","系统提示", MessageBoxButtons.OK,MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); 

              return false; 

          } 

      } 

 

 

      boolCheckValidCode(string input17, char last)//检查最后一位校验码 

      { 

          if (last ==CheckValidCode(input17)) 

              return true; 

          return false; 

      } 

      charCheckValidCode(string input17)//检查最后一位校验码 

      { 

          int sum = 0, cur; 

          for (int i = 0; i < 17; i++) 

          { 

              cur = Convert.ToInt32(input17[i])- 48; 

              sum += cur * weight[i]; 

          } 

          return vCode[(sum % 11)]; 

      } 

      bool GetAge(stringinput4)//算出年龄 

      { 

          try 

          { 

              age =Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(input4); 

              return true; 

          } 

          catch { return false; } 

      } 

      bool CheckACode(stringinput2)//检查地区码 

      { 

          if (address.IndexOf(input2) !=-1) 

              return true; 

          return false; 

       } 

       #endregion 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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、付费专栏及课程。

余额充值