Iphone UIDatePicker&;NSDateFormatter返回不正确的日期
我已经从一些帖子中尝试了各种解决方案,但没有运气。 问题是我面临的可能是时区。设备和模拟器上的UIDatePicker和NSDateFormatter都返回了错误的日期,从所选日期(根据设备或mac上设置的当前时区)添加或减去GMT 0的差值 我已经准备好设置语言环境和时区,但没有任何效果。 任何想法都是好主意!谢谢 (尚未发布任何代码示例,因为两个示例都已初始化,但未立即设置任何属性。) 更新1: 以下是代码片段:Iphone UIDatePicker&;NSDateFormatter返回不正确的日期,iphone,objective-c,ios4,uidatepicker,nsdateformatter,Iphone,Objective C,Ios4,Uidatepicker,Nsdateformatter,我已经从一些帖子中尝试了各种解决方案,但没有运气。 问题是我面临的可能是时区。设备和模拟器上的UIDatePicker和NSDateFormatter都返回了错误的日期,从所选日期(根据设备或mac上设置的当前时区)添加或减去GMT 0的差值 我已经准备好设置语言环境和时区,但没有任何效果。 任何想法都是好主意!谢谢 (尚未发布任何代码示例,因为两个示例都已初始化,但未立即设置任何属性。) 更新1: 以下是代码片段: datePicker = [[UIDatePicker alloc] ini
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, frame.size.height - 216.0, frame.size.width, 216.0)];
[self addSubview:datePicker];
....
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE, MMM d, y h:mma"];
NSString *dateString = [formatter stringFromDate:datePicker.date];
[formatter release];
你说你设定了时区,但你没说在哪里。您需要设置UIDatePicker和NSDateFormatter的时区属性。找到解决方案后,应按以下方式添加时区差异
- (NSDate *)dateToGMT:(NSDate *)sourceDate {
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate] autorelease];
return destinationDate;
}
发布你的代码。帮助我们帮助你!;-)我已经用代码更新了最初的帖子。我尝试将时区属性设置为:defaultTimeZone、localTimeZone,甚至指定GMT+3,但它们仍然返回错误的日期。