-1

我正在开发一个需要根据生日来查找某人年龄的应用程序。我有一个简单的NSDate但我怎么会找到这个NSDateFormatter

4

4 回答 4

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用于格式化日期(呃!)。根据经验,每当您需要对 a 执行一些计算时,NSDate您都可以使用 aNSCalendar和相关的类,例如NSDateComponents.

于 2013-10-05T20:02:39.113 回答
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]];
}
于 2014-12-16T17:02:25.687 回答
0

如果您只想知道某人的年龄,我找到了一种更短的方法来完成您想要完成的工作。

- (NSInteger)ageFromBirthday:(NSDate *)birthdate {
    NSDate *today = [NSDate date];
    NSInteger ageOfPerson = today.year - birthdate.year;
    return ageofPerson;
}
于 2016-03-18T13:57:08.950 回答
0

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

- (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", @"")];
    }
}
}
于 2016-04-08T07:33:25.990 回答