你好,我是 ios 的初学者,我已经创建了自定义 UIDatePicker 视图这显示正确的日期和时间但是当我们点击完成按钮时,这个显示日期正确但时间不正确....那么怎么做....
UIDatePicker *DatePicker;
UITexField *DateTime;
if ([[UIScreen mainScreen] bounds].size.height >480)
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,350,320,50)];
}
else
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,270,320,50)];
}
DatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;
DatePicker.datePickerMode = UIDatePickerModeDateAndTime;
DatePicker.date = [NSDate date];
[self.DateTime setInputView:DatePicker];
DatePicker.userInteractionEnabled=YES;
[self.view addSubview:DatePicker];
UIDatePicker *picker = (UIDatePicker*)self.DateTime.inputView;
self.DateTime.text = [NSString stringWithFormat:@"%@",picker.date];
NSLog(@"date picker is %@",self.DateTime.text);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss +HHmm"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@",DateTime.text]];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[anotherDateFormatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
NSLog(@"%@", [anotherDateFormatter stringFromDate:myDate]);
DateTime.text =[anotherDateFormatter stringFromDate:myDate];
NSLog(@"datetime is %@",DateTime.text);
这个选择器在 UI 中显示正确的时间和日期,但是当我们单击完成按钮(此处未提及)时,它会显示....DateTime.text=date picker is 2013-07-03 05:22:57 +0000
在这种格式之后它显示 2013-07-03 12:00:57 +0000
03/07/2013 12:00 AM
datetime is 03/07/2013 12:00 AM
当我们选择任何时间时,仅在时间上方显示...。日期是正确的...但是时间是错误的,所以...解决此问题