jeeva
jeeva

Reputation:

NSDateFormatter problem

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

Upvotes: 1

Views: 922

Answers (2)

Nate
Nate

Reputation: 19030

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

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

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

Upvotes: 0

Related Questions