iOS 中的日期/时间解析:如何处理(或不处理)时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5559912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date/Time parsing in iOS: how to deal (or not deal) with timezones?
提问by Gup3rSuR4c
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?
我有一个 ASP.NET MVC 3 网站,它通过 JSON 与我的 iOS 应用程序进行通信。作为在 JSON 响应中发送的对象的一部分,我的yyyy-MM-dd HH:mm:ss ZZZ
输出格式为日期2011-04-05 16:28:22 -07:00
。我如何在 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.
首先要注意的是,2011-04-05 16:28:22 -07:00
它必须看起来像2011-04-05T16:28:22-0700
,其中 aT
替换第一个空格(假设代表时间,或者字符串的时间部分从哪里开始?),第二个空格被删除,时区中的冒号是移除。我想我会找到一种方法来格式化 .NET 发送回的字符串以符合 iOS 将解析的字符串。
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 响应中发送的日期早 7 小时。所以,iOS 输出2011-04-05 16:28:22 -07:00
为2011-04-05 23:28:22 +0000
,就我的应用程序而言,这是错误的。
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.
到目前为止,我找到的唯一解决方案是将 JSON 中的日期发送为2011-04-05 16:28:22 +00:00
,但这又是错误的,因为我正在更改实际日期应该是什么。
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 通过格式yyyy-MM-dd HH:mm:ss ZZZ
(我认为可以重写yyyy-MM-ddTHH:mm:ssZZZ
)输出到NSDate
我可以在 iOS 中使用的对象。
采纳答案by Gup3rSuR4c
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:
我不知道这有多正确,但我最终发现在 .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"];
Only then is the date correct when NSLog
outputs, so I'm assuming that I've finally got a proper date/time.
只有这样,NSLog
输出时日期才正确,所以我假设我终于得到了正确的日期/时间。
回答by Anomie
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.
您的 iOS 日期解析代码是正确的:2011-04-05 16:28:22 -07:00
并且2011-04-05 23:28:22 +0000
表示相同的时间,只是在不同的时区。您唯一的问题是 NSDate 实际上并不存储时区,因此[date description]
使用 UTC 输出。
回答by Nicholas Riley
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.
几乎可以肯定,您最好只在任何地方使用 UTC 时间进行交换,而不必担心时区。我相信你可以让你的 ASP.NET 代码做到这一点,而且在接收端解析也更容易。NSDateFormatter 使用标准的 Unicode 日期格式语法,你可以在这里阅读。