コード例 #1
0
        /// <summary>
        /// 获取身份证中的生日,否则返回DateTime.MinValue
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static DateTime GetIDCardBirthday(string Id)
        {
            if (!ValidatorUtils.IsIDCard(Id))
            {
                return(DateTime.MinValue);
            }

            int intLen = Id.Length;

            if (intLen == 15)
            {
                string ts = string.Empty;
                if (Convert.ToInt32(Id.Substring(6, 2)) > 20)
                {
                    ts = "19";
                }
                else
                {
                    ts = "20";
                }
                string IDCode18 = Id.Substring(0, 6) + ts + Id.Substring(6);

                int[]    wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
                string[] wf = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
                int      s  = 0;
                for (int i = 0; i < 17; i++)
                {
                    s += wi[i] * Convert.ToInt32(IDCode18.Substring(i, 1));
                }
                IDCode18 += wf[Convert.ToInt32(s) % 11];

                Id = IDCode18;
            }

            if (Id.Length == 18)
            {
                string   birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
                DateTime time  = new DateTime();
                if (DateTime.TryParse(birth, out time) == false)
                {
                    return(DateTime.MinValue);//生日验证
                }
                return(time);
            }
            return(DateTime.MinValue);
        }