/// <summary> /// Метод возвращает строку с возрастом в формате: /// 0-1 | мес. /// 1-5 | год(-а)+мес. /// >5 | год(-а)(лет) /// </summary> public static string GetAgeString(DateDiff diff, string[] postfix) { string result = string.Empty; string FromZeroToOne = postfix[0]; string FromTwoToFour = postfix[1]; string MoreThanFour = postfix[2]; string LessThanYear = postfix[3]; int years = diff.Years; int mounths = diff.Months; if (years >= 5) {// Возраст больше 5 лет int tens = years % 10; int hundreds = years % 100; if (tens == 1 && hundreds != 11) {// 21,31,...101,121,131,..год (исключая 111 лет) result = String.Format("{0} {2}", years, mounths, FromZeroToOne); } else if (tens >= 2 && tens <= 4 && (hundreds < 10 || hundreds >= 20)) {//22,24,32,34,..102,104,122,124,...года result = String.Format("{0} {2}", years, mounths, FromTwoToFour); } else {// лет result = String.Format("{0} {1}", years, MoreThanFour); } } else if (years > 1 && years < 5) {// 2-4 года + мес. result = String.Format("{0} {1} {2} {3}", years, FromTwoToFour, mounths, LessThanYear); } else if (years == 1) {// 1 год + мес. result = String.Format("{0} {1} {2} {3}", years, FromZeroToOne, mounths, LessThanYear); } else if (years < 1) {// мес. result = String.Format("{0} {1}", mounths, LessThanYear); } return result; }
/// <summary> /// См. Требование #3900 /// </summary> public static string GetAgeString2(DateDiff diff, string[] postfix) { string result = string.Empty; string FromZeroToOne = postfix[0]; string FromTwoToFour = postfix[1]; string MoreThanFour = postfix[2]; string Mounths = postfix[3]; string Weeks = postfix[4]; int years = diff.Years; int mounths = diff.Months; if (years >= 3) {// если >36 месяцев то в годах int tens = years % 10; int hundreds = years % 100; if (tens == 1 && hundreds != 11) {// 21,31,...101,121,131,..год (исключая 111 лет) result = String.Format("{0} {2}", years, mounths, FromZeroToOne); } else if (tens >= 2 && tens <= 4 && (hundreds < 10 || hundreds >= 20)) {//22,24,32,34,..102,104,122,124,...года result = String.Format("{0} {2}", years, mounths, FromTwoToFour); } else {// лет result = String.Format("{0} {1}", years, MoreThanFour); } } else {// иначе - месяцы и недели int mounth = years * 12 + diff.Months; int days = diff.TotalDays; int weeks = (int)(days / 7); if (weeks >= 60) // месяцы result = String.Format("{0} {1}", mounth, Mounths); else // недели result = String.Format("{0} {1}", weeks, Weeks); } return result; }
public static string Get_Age(IPerson person) { string result = string.Empty; string[] postfix = { CaptionHelper.GetLocalizedText("Enums\\Age", "FromZeroToOne"),//год CaptionHelper.GetLocalizedText("Enums\\Age", "FromTwoToFour"),//года CaptionHelper.GetLocalizedText("Enums\\Age", "MoreThanFour") ,//лет CaptionHelper.GetLocalizedText("Enums\\Age", "Mounths"), //мес. CaptionHelper.GetLocalizedText("Enums\\Age", "Weeks") };//нед. DateTime today = DateTime.Now; DateTime min = today.AddYears(-150); DateTime dob = person.Birthday; if (dob > min && dob < today) { DateDiff diff = new DateDiff(today, dob); result = DateDiff.GetAgeString2(diff, postfix); } return result; }