How to convert timezone date string to NSDate

How to convert date string "2012-05-03 06:03:00 +0000" to NSDate. I am the user 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]; 
+4
source share
2 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 remember to set the dateZone date format you are targeting

+9
source

You can do this: -

 NSDateFormatter *datFormat = [[NSDateFormatter alloc] init]; [datFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Yout TimeZone"]]; [datFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSString *string = @"2012-05-03 06:03:00 +0000"; NSDate* date = [datFormat dateFromString:string]; 
0
source

Source: https://habr.com/ru/post/1412234/


All Articles