【Objective-c】小ネタ 年齢を計算する
さっきC#で書いたけど、やっぱりこっちでも必要になったので実装しましたw
- (NSInteger)getAgeByYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.year = year; components.month = month; components.day = day; NSDate *birthDay = [calendar dateFromComponents:components]; return [calendar components:NSYearCalendarUnit fromDate:birthDay toDate:[NSDate date] options:0].year; }