public class IDCard
{
/// <summary>
/// 性别
/// </summary>
public string Sex { get; set; }
/// <summary>
/// 性别编码 1 男 2 女
/// </summary>
public string SexCode { get; set; }
/// <summary>
/// 生日
/// </summary>
public string BirthDay { get; set; }
/// <summary>
/// 生肖
/// </summary>
public string ShengXiao { get; set; }
/// <summary>
/// 星座
/// </summary>
public string XingZuo { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int? Age { get; set; }
}
public IDCard DecodeIDCard(string keys)
{
IDCard card = new IDCard();
if (keys != null && CheckChinaIDCardNumberFormat(keys))
{
int sex = int.Parse(keys.Substring(16, 1));
if (sex % 2 == 0)
{
card.Sex = "女";
card.SexCode = "2";
}
else
{
card.Sex = "男";
card.SexCode = "1";
}
card.BirthDay = keys.Substring(6, 4) + "-" + keys.Substring(10, 2) + "-" + keys.Substring(12, 2);
card.ShengXiao = sx(keys.Substring(6, 4));
card.XingZuo = xz(keys.Substring(10, 2), keys.Substring(12, 2));
card.Age = CalculateAge(card.BirthDay);
}
return card;
}
/// <summary>
/// 获取生肖
/// </summary>
/// <param name="y">年</param>
/// <returns></returns>
public string sx(string y)
{
uint temp_y = uint.Parse(y);
temp_y = (temp_y - 1900) % 12;
string result = "";
switch (temp_y)
{
case 0:
result = "鼠";
break;
case 1:
result = "牛";
break;
case 2:
result = "虎";
break;
case 3:
result = "兔";
break;
case 4:
result = "龙";
break;
case 5:
result = "蛇";
break;
case 6:
result = "马";
break;
case 7:
result = "羊";
break;
case 8:
result = "猴";
break;
case 9:
result = "鸡";
break;
case 10:
result = "狗";
break;
case 11:
result = "猪";
break;
default:
result = "not found";
break;
}
//鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
return result;
}
/// <summary>
/// 获取星座
/// </summary>
/// <param name="mon">月</param>
/// <param name="day">日</param>
/// <returns></returns>
public string xz(string mon, string day)
{
if (mon == "01")
{
if (int.Parse(day) <= 19)
return "水瓶";
else
return "水瓶";
}
else if (mon == "02")
{
if (int.Parse(day) <= 19)
return "水瓶";
else
return "双鱼";
}
else if (mon == "03")
{
if (int.Parse(day) <= 20)
return "双鱼";
else
return "白羊";
}
else if (mon == "04")
{
if (int.Parse(day) <= 20)
return "白羊";
else
return "金牛";
}
else if (mon == "05")
{
if (int.Parse(day) <= 20)
return "金牛";
else
return "双子";
}
else if (mon == "06")
{
if (int.Parse(day) <= 21)
return "双子";
else
return "巨蟹";
}
else if (mon == "07")
{
if (int.Parse(day) <= 22)
return "巨蟹";
else
return "狮子";
}
else if (mon == "08")
{
if (int.Parse(day) <= 22)
return "狮子";
else
return "处女";
}
else if (mon == "09")
{
if (int.Parse(day) <= 22)
return "处女";
else
return "天平";
}
else if (mon == "10")
{
if (int.Parse(day) <= 22)
return "天平";
else
return "天蝎";
}
else if (mon == "11")
{
if (int.Parse(day) <= 21)
return "天蝎";
else
return "射手";
}
else
{
if (int.Parse(day) <= 21)
return "射手";
else
return "魔羯";
}
}
/// <summary>
/// 根据出生日期,计算精确的年龄
/// </summary>
/// <param name="birthDate">生日</param>
/// <returns></returns>
public static int CalculateAge(string birthDay)
{
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 age;
}
IDCard card=DecodeIDCard("身份证号")