在iOS中解析日期/时间:如何处理(或不处理)时区? [英] Date/Time parsing in iOS: how to deal (or not deal) with timezones?

查看:146
本文介绍了在iOS中解析日期/时间:如何处理(或不处理)时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]);

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

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.

真正的问题是那个日期输出比我在JSON响应中发送的时间早了7个小时。所以,iOS输出 2011-04-05 16:28:22 -07:00 as 2011-04-05 23:28:22 +0000 ,就我的应用而言,这是错误的。

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.

到目前为止,我发现的唯一解决方案是发送日期JSON为 2011-04-05 16:28:22 +00:00 ,但这又错了,因为我正在改变实际日期应该是什么。

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.

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

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.

推荐答案

我不知道知道这是对的,但我最终发现在.NET中我必须做 DateTime.ToUniversalTime()。ToString(yyyy-MM-ddHH:mm:ss)在iOS方面我必须这样做:

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"];

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

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

这篇关于在iOS中解析日期/时间:如何处理(或不处理)时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆