How to convert the Date String with time zone to NSDate

iphone, nsdate, objective-c

Solution

  NSString *dateStr = @"2012-05-03 06:03:00 +0000";
  NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
  [datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
  NSDate* date = [datFormatter dateFromString:dateStr];
  NSLog(@"date: %@", [datFormatter stringFromDate:date]);

This code prints `date: 2012-05-03 09:03:00 +0300`

It works. Also don't forget to set the date formatter's timeZone that you're targeting

Problem

How to convert the Date String "2012-05-03 06:03:00 +0000" to NSDate. I user the code below, but it does not work: ``` NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init]; [datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSDate* date = [datFormatter dateFromString:dateStr]; ```

Original source