Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the Date String with time zone to NSDate

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];
like image 866
itenyh Avatar asked May 13 '12 09:05

itenyh


1 Answers

  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

like image 97
Eugene Avatar answered Oct 13 '22 00:10

Eugene