根据身份证算出生日期和性别

public static bool ProcessIdCard(this string idCard, out DateTime birthday, out string genderName)
        {
            bool result;
            birthday = new DateTime();
            genderName = string.Empty;
            try
            {
                string sex;
                string birth;
                if (idCard.Length == 15)
                {
                    birth = idCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
                    sex = idCard.Substring(12, 3);
                }
                else
                {
                    birth = idCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
                    sex = idCard.Substring(14, 3);
                }
                genderName = int.Parse(sex) % 2 == 0 ? "" : "";
                result = DateTime.TryParse(birth, out birthday);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

 

posted @ 2014-12-04 15:17  VipSoft  阅读(313)  评论(0编辑  收藏  举报