转换前。 2010-09-11T00:00:00 + 01:00格式为NSDate [英] Converting ex. 2010-09-11T00:00:00+01:00 format to NSDate

查看:131
本文介绍了转换前。 2010-09-11T00:00:00 + 01:00格式为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了太多时间(超过一个小时)来完成我的两分钟任务。
在iPhone上:

I have spent way too much time (over an hour) on what I though would be a two minute task. On the iPhone:

NSString * dateString = @"2010-09-11T00:00:00+01:00";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssTZD"];
NSDate  *date = [formatter dateFromString:dateString];

结果:date == nil

RESULT: date == nil

我错过了什么! (除了我的截止日期)

What am I missing!! (Besides my deadline)

问候,

推荐答案

根据 unicode规范。您链接到其他地方的文档是某人向W3C提出的建议,仅供讨论。 Apple遵循的unicode标准是完整的标准,来自不同的机构。

TZD isn't a defined formatter per the unicode spec. The document you've linked to elsewhere was a suggestion someone made to W3C, for discussion only. The unicode standard followed by Apple is a finished standard, from a different body.

最接近你想要的是ZZZ(即@YYYY-MM- dd'T'HH:mm:ssZZZ),但中间没有冒号。所以你需要使用字符串:

The closest thing to what you want would be ZZZ (ie, @"YYYY-MM-dd'T'HH:mm:ssZZZ"), but that doesn't have a colon in the middle. So you'd need to use the string:

2010-09-11T00:00:00+0100

而不是你现在拥有的那个以+01:00结尾。

Rather than the one you currently have that ends in +01:00.

例如以下内容:

NSString * dateString = @"2010-09-11T00:00:00+0100";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
NSDate  *date = [formatter dateFromString:dateString];
NSLog(@"%@", date);

记录2010-09-10 23:00:00 GMT的有效日期对象。

Logs a valid date object, of 2010-09-10 23:00:00 GMT.

这篇关于转换前。 2010-09-11T00:00:00 + 01:00格式为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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