我有日期选择器来选择日期并将其显示在标签上。我正在尝试一种yyyy-MM-dd
. 但是当我选择任何日期时,标签不会显示我从日期选择器中选择的日期。
我怎样才能显示这种格式yyyy-MM-dd
?
这是我的代码:
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];
}
我的日期选择器如下所示: