0

我想将用户的年龄显示为字符串。我正在使用 Facebook API,我只看到生日日期,但看不到年龄值。是否可以将生日转换为年龄?这是我使用生日的代码:

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
                                                                    parameters:@{ @"fields" : @"id,birthday)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSString *ageOfLoginUser = [result valueForKey:@"birthday"];

            self.age.text = ageOfLoginUser;
        }
    }];
4

1 回答 1

0

正如上面评论中所说,您应该查看以下代码:如何根据 NSDate 计算年龄

NSString *birthDate = @"03/31/1990";    
NSDate *todayDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
int time = [todayDate timeIntervalSinceDate:[dateFormatter dateFromString:birthDate]];
int allDays = (((time/60)/60)/24);
int days = allDays%365;
int years = (allDays-days)/365;

NSLog(@"You live since %i years and %i days",years,days);
于 2015-08-11T19:26:56.167 回答