How to compute age from NSDate

2019-02-21 06:00发布

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?

4条回答
成全新的幸福
2楼-- · 2019-02-21 06:24

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]];
}
查看更多
混吃等死
3楼-- · 2019-02-21 06:28
- (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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-21 06:36

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", @"")];
    }
}
}
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-21 06:45

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;
}
查看更多
登录 后发表回答