【发布时间】:2016-05-26 19:04:55
【问题描述】:
我正在开发一款应用,该应用需要根据某人的生日来查找其年龄。我有一个简单的NSDate,但我如何使用NSDateFormatter 找到它?
【问题讨论】:
标签: ios nsdate nsdateformatter nsdatecomponents
我正在开发一款应用,该应用需要根据某人的生日来查找其年龄。我有一个简单的NSDate,但我如何使用NSDateFormatter 找到它?
【问题讨论】:
标签: ios nsdate nsdateformatter nsdatecomponents
- (NSInteger)ageFromBirthday:(NSDate *)birthdate {
NSDate *today = [NSDate date];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear
fromDate:birthdate
toDate:today
options:0];
return ageComponents.year;
}
NSDateFormatter 用于格式化日期(呃!)。根据经验,当您需要对 NSDate 执行一些计算时,您可以使用 NSCalendar 和相关的类,例如 NSDateComponents。
【讨论】:
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]];
}
【讨论】:
如果您只想知道某人的年龄,我找到了一种更短的方法来完成您想要完成的工作。
- (NSInteger)ageFromBirthday:(NSDate *)birthdate {
NSDate *today = [NSDate date];
NSInteger ageOfPerson = today.year - birthdate.year;
return ageofPerson;
}
【讨论】:
检查一下,我用过 “(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", @"")];
}
}
}
【讨论】: