Shaik Riyaz
Shaik Riyaz

Reputation: 11462

Not able to get NSDate from NSString

I tried executing the following code to get a date from an NSString:

NSDateFormatter * formatter =  [[NSDateFormatter alloc] init];

[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"GMT"]];

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

NSDate * date = [formatter dateFromString:@"2015-01-18T09:33:49.699-0600"];

NSLog(@"%@",date);

Why am I receiving nil value?

Upvotes: 0

Views: 80

Answers (1)

Rob
Rob

Reputation: 437552

The format string should be

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

Note the upper case .SSS.


And for the locale, you should specify @"en_US_POSIX" as advised in Technical Note TN1480. This will ensure that you can successfully parse this RFC 3339/ISO 8601 formatted date, regardless of the user's localization settings.

Upvotes: 3

Related Questions