I have date picker to select date and display it on a label. I'm trying a format of yyyy-MM-dd. But when I select any date the label does not show the date which I have selected from date picker.
How can I show this format yyyy-MM-dd?
Here is my code:
datepicker=[[UIDatePicker alloc]init];
datepicker.datePickerMode=UIDatePickerModeDate;
datepicker.backgroundColor = [UIColor grayColor];
[datepicker setValue:[UIColor whiteColor] forKey:@"textColor"];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 216 - 44, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor blackColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[toolBar setItems:[NSArray arrayWithObjects:doneBtn,space,nil]];
[_DOB setInputView: datepicker];
_DOB.inputAccessoryView =toolBar;
-(void)ShowSelectedDate {
NSDateFormatter *fromatter=[[NSDateFormatter alloc]init];
[fromatter setDateFormat:@"yyyy-MM-dd"];
self.DOB.text=[NSString stringWithFormat:@"%@",[fromatter stringFromDate:datepicker.date]];
[_DOB resignFirstResponder];
}
My date picker looks like this:

You can implement an action method for intercepting the
UIControlEventValueChangedof the date picker. Usually this is placed in the view controller class which manages the date picker:Add the action method to your picker as follows:
Where
selfis the view controller, which implementsdateChanged:. You can also use yourShowSelectedDatemethod for that:Note: Since
ShowSelectedDatetakes no parameters there is no colon at the end in@selector.You can also pass the date picker as parameter to
ShowSelectedDate:Then you should apply the action with:
Note the additional colon, because the method has now a parameter.