2013-10-05 87 views
-1

我正在研究一個需要根據生日來查找某人年齡的應用程序。我有一個簡單的NSDate但我怎麼會發現這與NSDateFormatter如何從NSDate計算年齡

+1

可能重複[如何基於日期計算年齡(http://stackoverflow.com/questions/4463893/how-to-calculate-the-age-based-on-date) – jww

回答

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用於格式化日期(duh!)。根據經驗,無論何時需要在NSDate上執行一些計算,都可以使用NSCalendar和關聯的類,如NSDateComponents

+0

謝謝!有效! –

2

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]]; 
} 
0

代替我發現了一個較短的方式做你正在嘗試完成,如果你只是想知道一個人有多老。

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

檢查這一點,我用 「(NSDateComponents *)成分:(NSCalendarUnit)unitFlags FROM日期:(NSDate的*)STARTINGDATE TODATE:(NSDate的*)resultDate選項:(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", @"")]; 
    } 
} 
}