I am working on an app that needs to find someones age depending on their birthday. I have a simple NSDate
but how would I find this with NSDateFormatter
?
Asked
Active
Viewed 4,015 times
-1

Gabriele Petronella
- 106,943
- 21
- 217
- 235

random_0620
- 1,636
- 5
- 23
- 44
-
1possible duplicate of [how to calculate the age based on date](http://stackoverflow.com/questions/4463893/how-to-calculate-the-age-based-on-date) – jww Sep 14 '14 at 20:21
4 Answers
24
- (NSInteger)ageFromBirthday:(NSDate *)birthdate {
NSDate *today = [NSDate date];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear
fromDate:birthdate
toDate:today
options:0];
return ageComponents.year;
}
NSDateFormatter
is for formatting dates (duh!). As a rule of thumb, whenever you need to perform some computation on an NSDate
you use a NSCalendar
and associated classes, such as NSDateComponents
.

karthikeyan
- 3,821
- 3
- 22
- 45

Gabriele Petronella
- 106,943
- 21
- 217
- 235
2
NSYearCalendarUnit is deprecated. It is replaced by 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]];
}

Jennifer Lim
- 31
- 2
0
I found a shorter way to do what you were trying to accomplish if you just want to know how old someone is.
- (NSInteger)ageFromBirthday:(NSDate *)birthdate {
NSDate *today = [NSDate date];
NSInteger ageOfPerson = today.year - birthdate.year;
return ageofPerson;
}

Yung Dai
- 158
- 1
- 5
0
Check this, I used "(NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts" and added localization
- (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", @"")];
}
}
}

Yigal Omer
- 301
- 1
- 2
- 6