NSString *birth = @"1993-03-03";
// 通过NSDateFormatter将NSString 转换成 NSDate 格式
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:birth];
// 通过NSDateComponents 从 date 中提取出 年月日
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
NSInteger year = [components1 year];
NSInteger month = [components1 month];
NSInteger day = [components1 day];
NSInteger hour = [components1 hour];
NSInteger minute = [components1 minute];
NSInteger second = [components1 second];
// 获取系统当前的年月日
NSDate *currentDate = [NSDate date]; // 获得系统的时间
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:currentDate];
NSInteger currentYear = [components2 year];
NSInteger currentMonth = [components2 month];
NSInteger currentDay = [components2 day];
// 计算年龄
NSInteger iAge = currentYear - year - 1;
if ((currentMonth > month) || (currentMonth == month && currentDay >= day)) {
iAge++;
}