UIDatePicker & NSDateFormatter 返回错误的日期
我已经尝试过一些帖子中的解决方案,但没有运气。 问题是我面临的可能是白色时区。设备和模拟器上的 UIDatePicker 和 NSDateFormatter 都返回不正确的日期,从所选日期(根据设备或 Mac 上设置的当前时区)添加或减去 GMT 0 的差异。
我已经尝试设置区域设置和时区,但没有任何效果。 任何想法都值得欢迎!谢谢。
(尚未发布任何代码示例,因为两者现在都已初始化而没有设置任何属性。)
更新 1:
这是代码片段:
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];
I have allready tryed solutions from a few posts, but no luck.
The problem is that i'm facing is probably whit time zones. Both on device and simulator UIDatePicker and NSDateFormatter are returning incorrect dates, adding or substracting the difference from GMT 0, from the selected date (according to the current time zone set on the device or mac).
I have allready tried setting locale and time zone but nothing worked.
Any idea is wellcomed! Thanks.
(Haven't posted any code sample because both are initialized without setting any properties right now.)
UPDATE 1:
Here is the code snippet:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了解决方案,应该按以下方式添加时区差异。
Found the solution, the time zone difference should be added in the following way.
你说你设置了时区,但你没有说在哪里。您需要设置 UIDatePicker 和 NSDateFormatter 的 timeZone 属性。
You said you set the time zone, but you didn't say where. You need to set the timeZone properties of your UIDatePicker and your NSDateFormatter.