tnblog
首页
视频
资源
登录

C# 获取身份证中的性别/年龄

4451人阅读 2022/3/24 16:19 总访问:1056728 评论:0 收藏:0 手机
分类: ASP.NET

获取身份证中的性别,代码如下:

public static bool GetGenderByIdCard(string idCard)
{
    if (string.IsNullOrWhiteSpace(idCard))
    {
        return false;
    }
    return Convert.ToBoolean(int.Parse(idCard.Substring(16, 1)) % 2);
}
获取身份证中的年龄,代码如下:
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;
}
————————————————
版权声明:本文为CSDN博主「vil du」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/My_ben/article/details/83985462


评价
没有个性,不需要签名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术