dateFromString returns nil after using rfc3339dateFormatter as an example in iOS Documentation

自古美人都是妖i 提交于 2019-12-12 04:44:41

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/25625183/datefromstring-returns-nil-after-using-rfc3339dateformatter-as-an-example-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!