UIDatePicker & NSDateFormatter 返回错误的日期

发布于 2024-09-26 12:28:23 字数 685 浏览 1 评论 0原文

我已经尝试过一些帖子中的解决方案,但没有运气。 问题是我面临的可能是白色时区。设备和模拟器上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2024-10-03 12:28:23

找到了解决方案,应该按以下方式添加时区差异。

- (NSDate *)dateToGMT:(NSDate *)sourceDate {
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate] autorelease];
    return destinationDate;
}

Found the solution, the time zone difference should be added in the following way.

- (NSDate *)dateToGMT:(NSDate *)sourceDate {
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate] autorelease];
    return destinationDate;
}
第几種人 2024-10-03 12:28:23

你说你设置了时区,但你没有说在哪里。您需要设置 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文