如何从NSDate计算年龄

我正在开发一个应用程序,需要根据他们的生日找到某人的年龄。 我有一个简单的NSDate但我怎么能用NSDateFormatter找到它?

 - (NSInteger)ageFromBirthday:(NSDate *)birthdate { NSDate *today = [NSDate date]; NSDateComponents *ageComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:birthdate toDate:today options:0]; return ageComponents.year; } 

NSDateFormatter用于格式化日期(呃!)。 根据经验,只要您需要在NSDate上执行某些计算,就可以使用NSCalendar和相关的类,例如NSDateComponents

NSYearCalendarUnit已弃用。 它被NSCalendarUnitYear取代

 - (NSString *) ageFromBirthDate:(NSString *)birthDate{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *myDate = [dateFormatter dateFromString: birthDate]; return [NSString stringWithFormat:@"%d ans", [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:myDate toDate:[NSDate date] options:0] year]]; } 

如果你只是想知道某人多大了,我找到了一个更短的方法去做你想要完成的事情。

 - (NSInteger)ageFromBirthday:(NSDate *)birthdate { NSDate *today = [NSDate date]; NSInteger ageOfPerson = today.year - birthdate.year; return ageofPerson; } 

检查一下,我用“ (NSDateComponents *)组件:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)选择 ”并添加了本地化

 - (NSString *) calculateAgeWith :(NSDate *)dateOfBirth { // if years == 0, dispaly months and days // if years > 0, display years and months NSDate *now = [NSDate date]; unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents* diff = [[NSCalendar currentCalendar] components:unitFlags fromDate:dateOfBirth toDate:now options:0]; NSInteger months = diff.month ; NSInteger days = diff.day ; NSInteger years = diff.year ; if (years == 0 && months == 0) { if (days == 1) { return [NSString stringWithFormat:@"%d %@", days, NSLocalizedString(@"day", @"")]; } else { return [NSString stringWithFormat:@"%d %@", days,NSLocalizedString(@"days", @"")]; } } else if (years == 0) { if (months == 1) { if (days == 0) { return [NSString stringWithFormat:@"1 %@%@",NSLocalizedString(@"and", @""), NSLocalizedString(@"month", @"")]; } else { return [NSString stringWithFormat:@"1 %@ %d %@",NSLocalizedString(@"month", @""),days,NSLocalizedString(@"days", @"")]; } } else { if (days == 0) { return [NSString stringWithFormat:@"%d %@", months,NSLocalizedString(@"months", @"")]; } else { return [NSString stringWithFormat:@"%d %@ %@%d %@", months,NSLocalizedString(@"months", @""),NSLocalizedString(@"and", @""),days,NSLocalizedString(@"days", @"")]; } } } else if ((years != 0) && (months == 0)) { if (years == 1) { return [NSString stringWithFormat:@"%d %@", years,NSLocalizedString(@"year", @"")]; } else { return [NSString stringWithFormat:@"%d %@", years,NSLocalizedString(@"years", @"")]; } } else { if ((years == 1) && (months == 1)) { return [NSString stringWithFormat:@"%@ %@%@",NSLocalizedString(@"one year", @""),NSLocalizedString(@"and", @""),NSLocalizedString(@"one month", @"")]; } else if (years == 1) { return [NSString stringWithFormat:@"%@ %@%d %@", NSLocalizedString(@"one year", @""),NSLocalizedString(@"and", @""), months,NSLocalizedString(@"months", @"")]; } else if (months == 1) { return [NSString stringWithFormat:@"%d %@ %@%@", years,NSLocalizedString(@"years", @""),NSLocalizedString(@"and", @""),NSLocalizedString(@"one month", @"")]; } else { return [NSString stringWithFormat:@"%d %@ %@%d %@", years,NSLocalizedString(@"years", @""),NSLocalizedString(@"and", @""), months,NSLocalizedString(@"months", @"")]; } } }