通过身份证号码取得生日的一段代码(支持18位和15位身份证)

    /// <summary>
    /// 根据身份证号码取得生日
    /// </summary>
    /// <param name="cardID">身份证号码</param>
    /// <returns>生日</returns>
    public string GetBirthday(string cardID)
    {
        string birthday;
        if (cardID.Length == 18)
        {
            string str_year = cardID.Substring(6, 4);
            string str_month = cardID.Substring(10, 2);
            string str_day = cardID.Substring(12, 2);

            int year = Convert.ToInt16(str_year);
            int month = Convert.ToInt16(str_month);
            int day = Convert.ToInt16(str_day);
            if ((year > 1800 && year < 2500) && (month <= 12) && (day <= 31))
            {
                birthday = year.ToString() + "年" + month.ToString() + "月" + day.ToString() + "日";
                return birthday;
            }
            else
            {
                RegisterStartupScript("提示", "<script> alert('获取生日时出错!')</script>");
                return null;
            }
        }
        else
        {
            string str_year = cardID.Substring(6, 2);
            string str_month = cardID.Substring(8, 2);
            string str_day = cardID.Substring(10, 2);

            int year = Convert.ToInt16(str_year);
            int month = Convert.ToInt16(str_month);
            int day = Convert.ToInt16(str_day);
            if ((month <= 12) && (day <= 31))
            {
                birthday = "19" + year.ToString() + "年" + month.ToString() + "月" + day.ToString() + "日";
                return birthday;
            }
            else
            {
                RegisterStartupScript("提示", "<script> alert('获取生日时出错!')</script>");
                return null;
            }
        }
    }

posted @ 2008-05-04 09:45  古道轻风  阅读(901)  评论(1编辑  收藏  举报