0

I have this NSString :

2010-05-29T16:31:49.000Z

And i want to convert it to NSDate with this method:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = nil;
[dateFormatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss.SSSZZZ"];
date = [dateFormatter dateFromString:dateSting];

And when i run it date is equal to Nil, Any idea why it happens?

It's happen only in iPad, and in iPhone it work perfectly.....

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • 1
    Just a stab in the dark: Do your iPad's time/date settings vary from the iPhone in *any way*? – Undo Jun 09 '13 at 12:30
  • Please check, I have edited my answer to meeting the ISO8601 way of parsing the date. – Anupdas Jun 09 '13 at 14:17

2 Answers2

4

A note from http://www.w3.org/TR/NOTE-datetime. Thanks to borrrden for bringing this to notice.

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").

NSString *inputString = @"2010-05-29T16:31:49.000Z";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSz"];

NSDate *date = [dateFormatter dateFromString:inputString];
Community
  • 1
  • 1
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • T is, but Z actually means "UTC" (as opposed to writing -01:00, etc). So be careful about this. This string is ISO8601, and should be parsed as such. See http://www.w3.org/TR/NOTE-datetime – borrrden Jun 09 '13 at 13:58
  • @borrrden Thanks for bringing this to notice. I have referred the dateFormats specified in [NSDateFormatter](http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns), couldn't find a counterpart so used a fallback. Please see if this is enough – Anupdas Jun 09 '13 at 14:16
  • 1
    Small letter z is enough, as I found out recently. In iOS 6+ there is a new specifier for ISO8601 (ZZZZZ), but that doesn't help for iOS 5. NSDateFormatters seems to understand the Z designator in date strings. Also, if all OP gets is "Z" then it doesn't matter if he parses it or ignores it really. However, I am concerned about future people reading this post. – borrrden Jun 09 '13 at 14:54
  • @Anupdas, Hi but this solution is not working in case of "2013-06-04T16:55:00Z" This string. Can you please help me. Thanks +1 – Mangesh Jun 17 '13 at 05:23
  • @Mangesh Can you verify if its `yyyy-MM-dd'T'HH:mm:ssz` or `yyyy-dd-MM'T'HH:mm:ssz`? – Anupdas Jun 17 '13 at 05:43
  • @Anupdas, it is "yyyy-MM-dd'T'HH:mm:ssz". Thanks for quick responce – Mangesh Jun 17 '13 at 05:50
  • @Mangesh I just worked it out, it is working fine. If you can send relevant code to my mail I can check what is going wrong. – Anupdas Jun 17 '13 at 05:52
  • Thanks for quick responce, Code is : http://stackoverflow.com/questions/17120767/need-to-convert-nsstring-to-nsdate/17120823#17120823 – Mangesh Jun 17 '13 at 06:04
  • @Mangesh I'm sorry to say this but, I don't find any others issues except the dataFormat. This is how I verified it `NSString * dateString = @"2013-06-04T16:55:00Z"; NSDateFormatter *formatter = [NSDateFormatter new]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"]; NSDate * date = [formatter dateFromString:dateString];` – Anupdas Jun 17 '13 at 06:10
0

This has been asked many times already. Your formatter is invalid for your input string:

yyyy-MM-dd'T'HH:mm:ss.SSSX

Not sure which one of the time-zone patterns you are using, so maybe XXX or XXXX should be used at the end.

See http://www.unicode.org/reports/tr35/tr35-dates.html. Note that Z is not allowed for time-zone pattern ZZZ.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Note that the formatter behavior changed between iOS 5 and iOS 6. You should always have a correct format pattern because you can't be sure if a slightly incorrect pattern will work or not. – Sulthan Jun 09 '13 at 12:44