Objective c 使用两个日期计算一个人的年龄

Objective c 使用两个日期计算一个人的年龄,objective-c,cocoa,time,nsdate,Objective C,Cocoa,Time,Nsdate,我代表一个人的类有一个计算该人年龄的方法: @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 - (NSDateCompon

我代表一个人的类有一个计算该人年龄的方法:

@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];

我应该如何处理此方法的结果?如何进行年龄计算:使用我已经设置的日期?

有几件事需要注意。首先,计算年龄的代码很好,只是我不明白为什么返回
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
而不是一个参数可以获得您之前设置的日期。

有几件事需要注意。首先,计算年龄的代码很好,只是我不明白为什么返回
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
而不是一个参数可以获取您之前设置的日期。

刚刚创建了一个简单的方法,以年为单位以字符串形式返回年龄

-(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格式的人的出生日期

  • 刚刚创建了一个简单的方法,以字符串形式返回年龄(以年为单位)

    -(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格式的人的出生日期

  • 这是一个经过测试的解决方案,在很长一段时间后,我有机会使用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;
    }
    

    这是一个经过测试的解决方案,在很长一段时间后,我有机会使用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;
    }
    

    非常感谢你,我根据你的建议调整了我的代码,现在它工作得很好。我真的很感激你的帮助,因为我完全被困住了。我不知道一个简单的年龄计算会有多复杂,并且能够把它称为一种方法,考虑到历法和闰年等方面的差异,现在有理由认为它不会那么容易!Gabriele,我正在编辑你的非常彻底的答案,以使它在使用后不再局限于该用户的特定代码。我真诚地希望这不会冒犯你@乔希·卡斯韦尔没有冒犯你。我同意现在答案和问题都更一般了,谢谢你的努力非常感谢你,我根据你的建议调整了我的代码,现在它工作得很好。我真的很感激你的帮助,因为我完全被困住了。我不知道一个简单的年龄计算会有多复杂,并且能够把它称为一种方法,考虑到历法和闰年等方面的差异,现在有理由认为它不会那么容易!Gabriele,我正在编辑你的非常彻底的答案,以使它在使用后不再局限于该用户的特定代码。我真诚地希望这不会冒犯你@乔希·卡斯韦尔没有冒犯你。我同意现在答案和问题都更一般了,谢谢你的努力为什么要在原始答案2.5年后发布重复答案?我同意原始答案非常有效!但我不会认为这是一个重复,因为它返回字符串中的答案。NSDate和NSString是两个不同的东西如果唯一的区别是在进行完全相同的计算后将年数转换为字符串,那么我称之为重复。这一细微的差别当然不值得为之发帖。特别是几年后的一个封闭式问题。我重视你说的话,你说的没错,我也在做同样的事情,为什么还要再写一遍呢我希望这些年来它能帮助一些人为什么在原始答案2.5年后发布重复答案?同意原始答案非常有效!但我不会认为这是一个重复,因为它返回字符串中的答案。NSDate和NSString是两个不同的东西如果唯一的区别是在进行完全相同的计算后将年数转换为字符串,那么我称之为重复。这一细微的差别当然不值得为之发帖。特别是几年后的一个封闭式问题。我重视你说的话,你说的没错,我也在做同样的事情,为什么还要再写一遍呢我希望这些年来它能帮助别人