Ejemplo n.º 1
0
        /// <summary>
        /// 将出生日期转换为年龄
        /// </summary>
        /// <param name="birthday">出生日期</param>
        /// <returns>年龄</returns>
        public static AgeValue GetAgeValue(DateTime birthday)
        {
            AgeValue age = new AgeValue();

            System.DateTime current = DateTime.Now;

            int _year   = current.Year - birthday.Year;
            int _month  = current.Month - birthday.Month;
            int _day    = current.Day - birthday.Day;
            int _hour   = current.Hour - birthday.Hour;
            int _minute = current.Minute - birthday.Minute;

            if (_minute < 0)
            {
                _hour   = _hour - 1;
                _minute = _minute + 60;
            }
            if (_hour < 0)
            {
                _day  = _day - 1;
                _hour = _hour + 24;
            }
            if (_day < 0)
            {
                _month = _month - 1;
                _day   = _day + DateTime.DaysInMonth(birthday.Year, birthday.Month);
            }
            if (_month < 0)
            {
                _year  = _year - 1;
                _month = _month + 12;
            }

            age.Y_num = _year;
            age.M_num = _month;
            age.D_num = _day;
            age.H_num = _hour;

            if (_year >= 15)
            {
                age.M_num = 0;
                age.D_num = 0;
                age.H_num = 0;
            }
            else if (_year >= 1)
            {
                age.D_num = 0;
                age.H_num = 0;
            }
            else if (_month >= 1)
            {
                age.H_num = 0;
            }
            return(age);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 将年龄转换为出生日期
        /// </summary>
        /// <param name="age">年龄</param>
        /// <returns>出生日期</returns>
        public static DateTime GetDateTime(AgeValue age)
        {
            System.DateTime date = DateTime.Now;

            date = date.AddYears((-1) * age.Y_num);

            date = date.AddMonths((-1) * age.M_num);

            date = date.AddDays((-1) * age.D_num);

            date = date.AddHours((-1) * age.H_num);

            return(date);
        }