Search code examples
iosdateuidatepickertarget-action

Date Format not working in IOS


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:

enter image description here


Solution

  • You can implement an action method for intercepting the UIControlEventValueChanged of the date picker. Usually this is placed in the view controller class which manages the date picker:

    -(IBAction)dateChanged:(UIDatePicker * )inPicker {
         // applying inPicker.date to your label
    }
    

    Add the action method to your picker as follows:

    datepicker=[[UIDatePicker alloc]init];
    datepicker.datePickerMode=UIDatePickerModeDate;
    [datepicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    

    Where self is the view controller, which implements dateChanged:. You can also use your ShowSelectedDatemethod for that:

    [datepicker addTarget:self action:@selector(ShowSelectedDate) forControlEvents:UIControlEventValueChanged];
    

    Note: Since ShowSelectedDate takes no parameters there is no colon at the end in @selector.

    You can also pass the date picker as parameter to ShowSelectedDate:

    -(void)ShowSelectedDate:(UIDatePicker * )datepicker {
        NSDateFormatter *fromatter=[[NSDateFormatter alloc]init];
        [fromatter setDateFormat:@"yyyy-MM-dd"];
        self.DOB.text=[NSString stringWithFormat:@"%@",[fromatter stringFromDate:datepicker.date]];
        [_DOB resignFirstResponder];
    }
    

    Then you should apply the action with:

    [datepicker addTarget:self action:@selector(ShowSelectedDate:) forControlEvents:UIControlEventValueChanged];
    

    Note the additional colon, because the method has now a parameter.