我代表一个人的班级有一种计算该人年龄的方法:
@interface Student : NSObject
@property (strong) NSDate *dob;
// This method will work out the person's age in calendar years
- (NSDateComponents*)ageCalculation:(NSDate*)dob;
这是类实现文件
@implementation Student
- (NSDateComponents*)ageCalculation:(NSDate*)dob {
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *components = [calendar components:units
fromDate:dob
toDate:now
options:0];
return components;
}
@end
我不确定我这样做是对的,但是:
Student *student1 = [[Student alloc] init];
[student1 setDob:aDateOfBirth];
NSDateComponents *ageComponents = [student1 ageCalculation:aDateOfBirth];
我该怎么做这个方法的结果?如何让ageCalculation:
使用我已设定的日期?
答案 0 :(得分:5)
有几点可以注意到。首先,用于计算年龄的代码很好,除了我不明白你为什么要返回NSDateComponents
而不仅仅是几年的年龄。
以下是我将如何做到这一点
- (NSInteger)age {
NSDate *today = [NSDate date];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit
fromDate:self.dob
toDate:today
options:0];
return ageComponents.year;
}
您只对year
组件感兴趣,因此请将其返回NSDateComponents
。使用self.dob
而不是参数获取您之前设置的日期。
答案 1 :(得分:1)
这里是经过测试的解决方案,经过很长一段时间,我有机会使用Objective C,并尝试从出生日期开始查找年龄,然后编写了此解决方案,希望可以对某人有所帮助
Call this Function as : [self getAge:datePicker.date]
- (NSString *)getAge:(NSDate *)dateOfBirth {
NSInteger years;
NSInteger months;
NSInteger days;
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:dateOfBirth toDate:[NSDate date] options:0];
years = [dateComponent year];
months = [dateComponent month];
days = [dateComponent day];
NSString *ageStr = [[NSString alloc]init];
if (years > 0) {
if (years > 1) {
ageStr = [NSString stringWithFormat:@"%ld years",years];
}
else {
ageStr = [NSString stringWithFormat:@"%ld year",years];
}
}
if (months > 0) {
if (months > 1) {
if(![ageStr isEqualToString:@""]) {
ageStr = [NSString stringWithFormat:@"%@ %ld months",ageStr,months];
}
else {
ageStr = [NSString stringWithFormat:@"%ld months",months];
}
}
else {
if(![ageStr isEqualToString:@""]) {
ageStr = [NSString stringWithFormat:@"%@ %ld month",ageStr,months];
}
else {
ageStr = [NSString stringWithFormat:@"%ld month",months];
}
}
}
if (days > 0) {
if (days > 1) {
if(![ageStr isEqualToString:@""]) {
ageStr = [NSString stringWithFormat:@"%@ %ld days",ageStr,days];
}
else {
ageStr = [NSString stringWithFormat:@"%ld days",days];
}
}
else {
if(![ageStr isEqualToString:@""]) {
ageStr = [NSString stringWithFormat:@"%@ %ld day",ageStr,days];
}
else {
ageStr = [NSString stringWithFormat:@"%ld day",days];
}
}
}
return ageStr;
}
答案 2 :(得分:-1)
刚刚创建了一个简单的方法,可以将年龄作为字符串返回。
-(NSString *) getAge:(NSDate *)dateOfBirth {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierIndian];
NSDateComponents *components = [calendar components:NSYearCalendarUnit
fromDate:dateOfBirth
toDate:[NSDate date]
options:0];
return [NSString stringWithFormat:@"%li",components.year];
}
注意强>
NSString *yearsOldinString = [self getAge:dateOfBirth];
,其中dateOfBirth以NSDate格式表示出生日期。