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