iOS 中的日期/时间解析:如何处理(或不处理)时区?

发布于 2024-10-31 05:34:29 字数 1152 浏览 3 评论 0原文

我有一个 ASP.NET MVC 3 网站,它通过 JSON 与我的 iOS 应用程序进行通信。作为 JSON 响应中发送的对象的一部分,我的日期格式为 yyyy-MM-dd HH:mm:ss ZZZ ,其输出 2011-04-05 16:28: 22 -07:00。我如何在 iOS 中解析它?

这是我现在正在处理的代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:@"2011-04-05T16:28:22-0700"];

NSLog(@"%@; %@; %@", dateFormatter, date, [NSTimeZone localTimeZone]);

首先要注意的是 2011-04-05 16:28:22 -07:00 必须看起来像 2011-04 -05T16:28:22-0700,其中 T 替换第一个空格(假设代表时间,或者字符串的时间部分从哪里开始?),第二个空格被删除并且时区中的冒号被删除。我想我会找到一种方法来格式化 .NET 发送回的字符串,以符合 iOS 将解析的字符串。

真正的问题是输出的日期比我在 JSON 响应中发送的日期早 7 小时。因此,iOS 将 2011-04-05 16:28:22 -07:00 输出为 2011-04-05 23:28:22 +0000,这是错误的就我的应用程序而言。

到目前为止,我找到的唯一解决方案是将 JSON 中的日期发送为 2011-04-05 16:28:22 +00:00,但这又是错误的,因为我正在更改真正的日期应该是什么。

不管怎样,我很感激有人看一下并让我知道如何解析 .NET 通过格式 yyyy-MM-dd HH:mm:ss ZZZ 输出的日期字符串(我想可以重写为 yyyy-MM-ddTHH:mm:ssZZZ )到我可以在 iOS 中使用的 NSDate 对象。

I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ which outputs 2011-04-05 16:28:22 -07:00. How do I parse that in iOS?

This is the code I'm messing around with right now:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:@"2011-04-05T16:28:22-0700"];

NSLog(@"%@; %@; %@", dateFormatter, date, [NSTimeZone localTimeZone]);

First thing to note is that 2011-04-05 16:28:22 -07:00 has to look like 2011-04-05T16:28:22-0700, where a T replaces the first space (assuming that stands for time, or where the time part of the string starts from?), the second space is removed and the colon in the time zone is removed. I figure I'll find a way to format the string that .NET is sending back to conform to the string iOS will parse.

The real issue is that the date that is outputted is 7 hours ahead of what I've sent in the JSON response. So, iOS outputs 2011-04-05 16:28:22 -07:00 as 2011-04-05 23:28:22 +0000, and that is wrong as far as my app is concerned.

The only solution I've found so far is to send the date in the JSON as 2011-04-05 16:28:22 +00:00, but that again is wrong because I'm altering what the real date should be.

Anyway, I'd appreciate someone taking a look and letting me know how I can parse the date string .NET is outputting via the format yyyy-MM-dd HH:mm:ss ZZZ (which I suppose can be re-written to yyyy-MM-ddTHH:mm:ssZZZ) to an NSDate object I can use in iOS.

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

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

发布评论

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

评论(3

染年凉城似染瑾 2024-11-07 05:34:29

我不知道这有多正确,但我最终发现在.NET中我必须执行 DateTime.ToUniversalTime().ToString("yyyy-MM-ddHH:mm:ss") 和在 iOS 端,我必须这样做:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2011-04-0600:28:27"];

只有这样,NSLog 输出时日期才是正确的,所以我假设我终于得到了正确的日期/时间。

I don't know how right this is, but I ultimately found that in .NET I have to do DateTime.ToUniversalTime().ToString("yyyy-MM-ddHH:mm:ss") and on the iOS side I have to do this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2011-04-0600:28:27"];

Only then is the date correct when NSLog outputs, so I'm assuming that I've finally got a proper date/time.

贵在坚持 2024-11-07 05:34:29

您的 iOS 日期解析代码是正确的: 2011-04-05 16:28:22 -07:002011-04-05 23:28:22 +0000 代表同一时间,只是在不同的时区。您唯一的问题是 NSDate 实际上并不存储时区,因此 [date description] 使用 UTC 输出。

Your iOS date parsing code is correct: 2011-04-05 16:28:22 -07:00 and 2011-04-05 23:28:22 +0000 represent the same time, just in different time zones. Your only problem is that NSDate doesn't actually store the time zone, and so [date description] outputs using UTC.

不念旧人 2024-11-07 05:34:29

几乎可以肯定,您最好在任何地方都使用 UTC 时间进行交换,而不用担心时区。我确信您可以使用 ASP.NET 代码来执行此操作,并且在接收端也更容易进行解析。 NSDateFormatter 使用标准 Unicode 日期格式化语法,您可以在此处阅读该语法。

You're almost certainly better off just using UTC time everywhere for interchange and not bothering with time zones. I'm sure you can get that your ASP.NET code to do that, and it's easier to parse on the receiving end as well. NSDateFormatter uses standard Unicode date formatting syntax, which you can read about here.

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