static int AgeDifference(AgeRangeType ageType, System.DateTime startDate, System.DateTime endDate) { int diff = 0; var ageInYears = endDate.Year - startDate.Year; if (startDate > endDate.AddYears(-ageInYears)) ageInYears -= 1; var newStart = startDate.AddYears(ageInYears); switch (ageType) { case AgeRangeType.Months: var monthsOld = 0; while (newStart.AddMonths(monthsOld + 1) <= endDate.Date) { monthsOld++; } diff = ageInYears * 12 + monthsOld; break; case AgeRangeType.Weeks: var TS = endDate.Date - newStart.Date; diff = ageInYears * 52 + (TS.Days / 7); break; default: var age = endDate.Year - startDate.Year; if (startDate > endDate.AddYears(-age)) age -= 1; diff = age; break; } return diff; }
/// <summary> /// Comprueba que el valor de la fecha que se pasa en el parámetro FechaNac sea tan atrás en el tiempo como /// años se especifiquen en el Web.config para hacer uso de este servicio. /// </summary> /// <param name="FechaNac"></param> /// <returns></returns> public static bool valFechaNac(System.DateTime FechaNac) { if (FechaNac.AddYears(wsSettings.MinAgeToUseThisService) > System.DateTime.Now) { return false; } return true; }