首页 > Windows开发 > 详细

C#获取年龄段 几零后

时间:2015-11-18 12:25:55      阅读:362      评论:0      收藏:0      [点我收藏+]
/// <summary>
        /// 根据年龄获得年龄段
        /// </summary>
        /// <param name="age"></param>
        /// <returns></returns>
        public static string GetAgeRange(int age)
        {
            int year = DateTime.Now.Year - age;
            if (year <= 1900 || year > DateTime.Now.Year)
            {
                return "";
            }
            string thirdNum= year.ToString().Substring(2, 1);
            string fourthNum = year.ToString().Substring(3, 1);

            if (int.Parse(fourthNum) >= 5)
            {
                return string.Format("{0}{1}后", thirdNum, "5");
            }
            else
            {
                return string.Format("{0}{1}后", thirdNum, "0");
            }
        }
        /// <summary>
        /// 根据身份证获得年龄段
        /// </summary>
        /// <param name="IDCard"></param>
        /// <returns></returns>
        public static string GetAgeRange(string IDCard)
        {
            DateTime birth = ValidatHelper.GetBirthByIDCard(IDCard);
            string year = birth.Year.ToString().PadLeft(4, 0);
            string thirdNum= year.ToString().Substring(2, 1);
            string fourthNum= year.ToString().Substring(3, 1);

            if (int.Parse(y2) >= 5)
            {
                return string.Format("{0}{1}后", thirdNum, "5");
            }
            else
            {
                return string.Format("{0}{1}后", thirdNum, "0");
            }
        }
        #region 通过身份证号获得出生日期
        public static DateTime GetBirthByIDCard(string IDCard)
        {
            if (!CheckIDCard(IDCard))
            {
                return DateTime.MinValue.Date;
            }
            string birth = "";
            if (IDCard.Length == 18)
            {
                birth = IDCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            }
            else if (IDCard.Length == 15)
            {
                birth = IDCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            }
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == true)
            {
                return time;
            }
            else
            {
                return DateTime.MinValue.Date;
            }
        }
        #endregion

 

C#获取年龄段 几零后

原文:http://www.cnblogs.com/ericli-ericli/p/4973788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!