public static int IsAdult(string input)
{
if (!string.IsNullOrEmpty(input) && !string.IsNullOrWhiteSpace(input))
{
if (input.Length != 15 && input.Length != 18)
{
return 0;
}
string birthday;
if (input.Length == 18)
{
birthday = input.Substring(6, 4) + "-" + input.Substring(10, 2) + "-" + input.Substring(12, 2);
}
else
{
birthday = "19" + input.Substring(6, 2) + "-" + input.Substring(8, 2) + "-" + input.Substring(10, 2);
}
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 18 <= age ? 1 : 2;
}
else
{
return 0;
}
}
C# 根据身份证号计算是否成年
发布于
2020-04-11
|
1分钟
|
112字数
|
浏览量:
- 本文作者: 卢敏强的博客
- 本文链接: C# 根据身份证号计算是否成年
- 版权声明: 本博客所有文章除特别声明外,转载请注明出处!
0%
召唤伊斯特瓦尔