ios - use uidatepicker to set textfield value -
i have uidatepicker
stores date selected textfield. want text field calculates how old person (as whole int) depending on date entered, e.g. "15". unsure how that. point me in right direction please . here's have 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 @ todays date, subtract date in birthdaytextfield calculate age maybe? }
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]; }
Comments
Post a Comment