使用rfc3339dateFormatter作为iOS文档中的一个示例,dateFromString返回nil [英] dateFromString returns nil after using rfc3339dateFormatter as an example in iOS Documentation

查看:716
本文介绍了使用rfc3339dateFormatter作为iOS文档中的一个示例,dateFromString返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSDateFormatter * rfc3339DateFormatter = [[NSDateFormatter alloc]我想通过以下方式解析NSString来获取NSDate:在里面]; 
NSLocale * enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@en_US_POSIX];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'Z'];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString * createdString = [NSString stringWithFormat:@2014-08-27T17:10:3​​6.000 + 05:30];
NSDate * createdDate = [rfc3339DateFormatter dateFromString:createdString];

这里createdDate被返回为nil。我在这里缺少什么?另外请澄清我正确使用timeZone和locale。

解决方案

您没有使用Right Date Formatter



使用此行:

  [rfc3339DateFormatter setDateFormat:@yyyy-MM-dd'T' HH:mm:ss.SSSZZZZZ]; 

代替:

  [rfc3339DateFormatter setDateFormat:@yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'Z']; 


I am trying to get NSDate from NSString by parsing in following manner:

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString *createdString = [NSString stringWithFormat:@"2014-08-27T17:10:36.000+05:30"];
NSDate *createdDate = [rfc3339DateFormatter dateFromString:createdString];

Here createdDate is being returned as nil. What am I missing here? Also please clarify me proper usage of timeZone and locale.

解决方案

You are not using Right Date Formatter

Use This Line:

[rfc3339DateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];

in Place of this:

[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

这篇关于使用rfc3339dateFormatter作为iOS文档中的一个示例,dateFromString返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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