YosiFZ
YosiFZ

Reputation: 7900

NSDateFormatter in iPad issue

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.....

Upvotes: 0

Views: 459

Answers (2)

Anupdas
Anupdas

Reputation: 10201

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

Upvotes: 4

Sulthan
Sulthan

Reputation: 130191

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.

Upvotes: 0

Related Questions