Reputation: 433
My class representing a person has a method to calculate that person's age:
@interface Student : NSObject
@property (strong) NSDate *dob;
// This method will work out the person's age in calendar years
- (NSDateComponents*)ageCalculation:(NSDate*)dob;
Here is the class implementation file
@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
I'm not sure that I'm doing this right, though:
Student *student1 = [[Student alloc] init];
[student1 setDob:aDateOfBirth];
NSDateComponents *ageComponents = [student1 ageCalculation:aDateOfBirth];
What should I do with the result of this method? How can I make ageCalculation:
use the date I already set?
Upvotes: 1
Views: 2788
Reputation: 361
Here is tested solution, After long time I get a chance to work with Objective C and trying to find age from date of birth then I write this solution, Hopefully this help someone
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;
}
Upvotes: 1
Reputation: 2336
Just created a simple method that returns the age in years as a string.
-(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];
}
NOTE
NSString *yearsOldinString = [self getAge:dateOfBirth];
, where dateOfBirth represents person's date of birth in NSDate format.Upvotes: -1
Reputation: 108141
There are a couple of things that can be noted. First of all, the code for computing the age is fine, except that I don't understand why you return NSDateComponents
instead of just the age in years.
Here's how I would do it
- (NSInteger)age {
NSDate *today = [NSDate date];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit
fromDate:self.dob
toDate:today
options:0];
return ageComponents.year;
}
You are interested only in the year
component, so return it instead of NSDateComponents
. Using self.dob
instead of an argument gets the date you set earlier.
Upvotes: 5