0

I want to display age of user to string. I am working with Facebook API and I see only date of birthday but not age value. Is it possible to convert bithday to age? This is my code using birthday:

[[[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;
        }
    }];
Luki
  • 437
  • 2
  • 6
  • 15
  • 1
    this question has the possible answer here: http://stackoverflow.com/questions/4463893/how-to-calculate-the-age-based-on-date/6103037#6103037 you just need to convert the data. – Guilherme Caraciolo Aug 11 '15 at 16:59
  • What does return exactly `[result valueForKey:@"birthday"]`? – Larme Aug 11 '15 at 19:26

1 Answers1

0

As said in the comment above you should look at this code from: How to calculate the age based on 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);
Community
  • 1
  • 1
Wyetro
  • 8,439
  • 9
  • 46
  • 64