创建 NSDate 时区问题

发布于 2024-12-13 15:28:42 字数 1770 浏览 2 评论 0原文

我正在从我的网络服务加载日期,我正在发送以下格式的日期(GMT 时间): 02/11/11 10:56:09

我正在使用 NSDateFormatter 创建一个 NSDate 表单,如下所示:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];

这非常有用,之后我将其与当前日期进行比较以获得相对时间间隔。

问题是,当手机设置在不同的时区时,当我从 api 加载日期并使用日期格式化程序时,似乎发生的情况是手机假设日期字符串是当地时间并对其进行转换到格林威治标准时间。

示例:

我从 api 加载时间为上午 10 点的日期。 电话设置为 PDT。 日期格式化程序正在创建一个 NSDate,假设我的日期字符串是上午 10 点,实际上与手机相关。

我最终得到的日期和时间等于下午 5 点,加上 10 个小时。

我试图在日期格式化程序中指定字符串为 GMT,但我遇到了麻烦,我尝试了以下操作,将 GMT 添加到格式中:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss GMT"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];

这不起作用。

任何人都可以提供任何建议吗?

解决方案

简单回顾一下,我通过将 GMT 附加到原始字符串并格式化它来解决了一个糟糕的问题:

NSString * cheat = [NSString stringWithFormat:@"%@ GMT", str];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss zzzz"];
NSDate *journeyDate = [dateFormatter dateFromString:cheat];
[dateFormatter release];
return journeyDate;

这是一种不稳定的黑客,因为如果该字符串更改为包含时区,它不再起作用。对于任何需要像我一样的人,下面只是一个关于如何创建 NSTimeZone 的快速示例。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
return journeyDate;

感谢您的快速帮助。

I am loading in dates from my web service, I'm sending dates in the format (GMT times): 02/11/11 10:56:09

I am creating an NSDate form this using NSDateFormatter as such:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];

This works great, after I'm comparing this to the current date to get relative time intervals.

The problem is when the phone is set up in a different timezone, when I load in the date from my api, and use the date formatter, what seems to be happening is the phone is assuming the date string is local time and it's converting it to GMT.

Example:

I load in a date with the time 10am from the api.
The phone is set to PDT.
The date formatter is creating an NSDate assuming that my date string with 10am, is actually relevant to the phone.

I end up with a date and time equal to 5pm, adding 10 hours.

I am trying to specify in my date formatter that the string is GMT, but I'm having trouble, I've tried the following, adding GMT to the format:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss GMT"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];

This is not working.

Can anyone give any advice ?

Solution

Just a recap, I got it working with a terrible work around by appending GMT to the original string, and formatting that:

NSString * cheat = [NSString stringWithFormat:@"%@ GMT", str];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss zzzz"];
NSDate *journeyDate = [dateFormatter dateFromString:cheat];
[dateFormatter release];
return journeyDate;

This was a kind of unstable hack, because if the string changed to include a timezone, it wouldn't work anymore. For anyone who needs to do as myself, the following is just a quick example on how to create an NSTimeZone.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
return journeyDate;

Thanks for the quick help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心清如水 2024-12-20 15:28:42

我怀疑你只是想使用 NSDateFormatter.setTimeZone 强制使用 世界标准时间。您不想更改格式字符串,因为该字符串可能不包含字母“GMT” - 相反,您希望更改解释该字符串的时区,这就是< code>setTimeZone 就可以了。

I suspect you just want to use NSDateFormatter.setTimeZone to force it to use UTC. You don't want to change the format string because presumably the string doesn't include the letters "GMT" - instead, you want to change which time zone the string is interpreted in, which is what setTimeZone will do.

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