Unable to Parse Date using NSDateFormatter

2019-03-30 04:21发布

I am fetching a RSS, in which i receive the following Date stamp:

2010-05-10T06:11:14.000Z

Now i am using NSDateFormatter to parse this datetime stamp.

[parseFormatter setDateFormat:@"yyyy-MM-dTH:m:s.z"];

But its not working fine if just remove the time stamp part it works for the date [parseFormatter setDateFormat:@"yyyy-MM-d"]; But if i add the rest of the stuff it returns nil.

Any idea ?

Thanks in Advance....

2条回答
贼婆χ
2楼-- · 2019-03-30 05:06

You need to parse zero padded values for d, H, m, s

You need to escape the literal T as 'T'

You need to parse the fractional part of the seconds with SSS

You can accept a literal Z with 'Z' or use uppercase Z to try and parse the timezone but RSS uses separate standard.

@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Date Format Patterns

查看更多
小情绪 Triste *
3楼-- · 2019-03-30 05:15

From memory I've had this problem in the past as well. A couple of things to try:

  1. Replace the "T" in your RSS feed with a space (and obviously change the date format string as well).

  2. Your RSS feed has padded digits, where as your format string does not. Give this a try "yyyy-MM-ddTHH:mm:ss.Z". According to this link, that "z" should be capilized. http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/

  3. Strip off the timezone info from the RSS feed and parse this manually. I seem to remember this causing problems for me.

查看更多
登录 后发表回答