set datepicker date on textfield
I'm using core data to add an entity to a database. There is a field for
calculating your age. You use the picker to fill out your birthday and it
sets the text of the textfield according to what was selected in the
datepicker. There is another view, a detail view of the entity which you
can edit. Unfortunately, when the picker loads, it loads todays date, and
not the date in the textfield that was already set. Is there any way I can
set the picker's date using a textfield? The text is in this format:
August 11, 1997. Sorry if I explained poorly. Basically I want to set the
datepicker's date based upon the text in a textfield.
just so you have an idea of what is happening:
- (void) dateChanged:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
birthdayTextField.text = [NSString stringWithFormat:@"%@",
[dateFormatter stringFromDate:birthdayDatePicker.date]];
[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];
if(components.year > 0){
ageTextField.text=[NSString stringWithFormat:@"%d",components.year];
}
}
No comments:
Post a Comment