0

你好,我是 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

当我们选择任何时间时,仅在时间上方显示...。日期是正确的...但是时间是错误的,所以...解决此问题

4

3 回答 3

1

此代码可能对您有所帮助

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    //[formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"dd-MM-yyyy hh:mm"];
    strDatetime = [formatter stringFromDate:picker.date];
    [datetimeLbl setText:strDatetime];
于 2013-07-03T07:14:12.040 回答
0

这可能是 NSTimeZone 的问题。

你可以试试这个

NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];

NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];

NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[df_local setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];

NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
于 2013-07-03T06:51:02.933 回答
0
 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
 [outputFormatter setDateFormat:@"HH:mm"]; //24hr time format
 NSString *dateString = [outputFormatter stringFromDate: picker.date];

 NSLog(@"%@",dateString);

它将打印正确的时间。

于 2013-07-03T06:15:02.223 回答