Convert birthday to age vale in Facebook API iOS

621 Views Asked by At

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;
        }
    }];
1

There are 1 best solutions below

2
On

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);