How to compute age from NSDate

一个人想着一个人 提交于 2019-12-01 13:31:02

问题


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?


回答1:


- (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.




回答2:


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:


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



回答4:


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", @"")];
    }
}
}


来源:https://stackoverflow.com/questions/19202120/how-to-compute-age-from-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!