views:

162

answers:

2

I am trying to get NSDate from string but its returning nil. I tried this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormatter dateFromString:@"2010-05-07T10:12:32UTC"];
NSLog(@"Success date=%@",[date description]);

Thanks

A: 

The literal 'Z' won't match the string UTC.

Marcelo Cantos
A: 

Your date format string expects the date to end with a literal Z. But your date ends with the string UTC. There are several ways to fix this.

  1. Change your date format string to @"yyyy-mm-dd'T'HH:mm:ss'UTC'".
  2. Or, change your date string to @"2010-05-07T10:12:32Z".

Or, you could change your date format string to: @"yyyy-mm-dd'T'HH:mm:ssz". This will recognize some common 3-letter time zone names, such as PDT for Pacific Daylight Time. Unfortunately, however, it will not recognize UTC as a time zone name (you’d have to use “GMT”).

Nate