1

I have a UIDatePicker that stores the date currently selected into a textfield. I want another text field that calculates how old a person (as a whole int) would be depending on the date entered, e.g. "15". but am unsure how to do that. could anyone point me in the right direction please . Here's what I have so far.

-(void)viewDidAppear:(BOOL)animated{
    [birthdayDatePicker addTarget:self
                   action:@selector(dateChanged:)
         forControlEvents:UIControlEventValueChanged];
}

- (void) dateChanged:(id)sender{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterLongStyle;
    birthdayTextField.text = [NSString stringWithFormat:@"%@",
                   [dateFormatter stringFromDate:birthdayDatePicker.date]];
    //look at todays date, subtract date in birthdayTextField to calculate age maybe?

}
Wil Shipley
  • 9,343
  • 35
  • 59
Josue Espinosa
  • 129
  • 3
  • 10
  • http://hamishrickerby.com/2010/01/07/calculate-age-in-objective-c/ – CRDave Aug 08 '13 at 05:52
  • http://iosdevblog.com/2012/03/22/calculate-age-from-an-nsdate-in-objective-c/ – CRDave Aug 08 '13 at 05:53
  • http://stackoverflow.com/questions/4463893/how-to-calculate-the-age-based-on-date – CRDave Aug 08 '13 at 05:53
  • http://www.geekslivelonger.com/calculate-age-from-date-of-birth/ – CRDave Aug 08 '13 at 05:54
  • http://stackoverflow.com/questions/5562594/difference-between-two-nsdate-objects-result-also-a-nsdate – CRDave Aug 08 '13 at 05:54
  • Search internet properly before asking question. https://www.google.co.in/search?q=Objective+C++calculate+age+from+NSDate&oq=Objective+C++calculate+age+from+NSDate&aqs=chrome.0.69i57j69i60l2j69i62l3.16699j0&sourceid=chrome&ie=UTF-8 – CRDave Aug 08 '13 at 05:55

2 Answers2

1

just after you finish getting the entered date is set, you can now compute the age based on todays date. you can read on how to compute date intervals using following links

Objective C - calculating the number of days between two dates

or

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/DatesAndTimes/Articles/dtDates.html

Community
  • 1
  • 1
Mehul Rathod
  • 1,244
  • 8
  • 7
1

You can try this

- (void) dateChanged:(id)sender{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *startdate= [NSString stringWithFormat:@"%@",
                              [dateFormatter stringFromDate: birthdayDatePicker.date]];

    NSDate *End=[dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSYearCalendarUnit
                                                        fromDate:[dateFormatter dateFromString:startdate]
                                                          toDate:End
                                                         options:0];
 birthdayTextField.text=[NSString stringWithFormat:@" %d years old",components.year];

}
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121