Specify a time offset using NSDateFormatter (-0500)

I have date strings similar to these:

2013-01-25 00:00:00 -0500 2013-01-22 00:00:00 -0700 2013-01-26 00:00:00 -0200 

I want to use NSDateFormatter to create an NSDate using these string types. I know how to use the formatter to get the first part of the date ( 2013-01-25 00:00:00 ), but I do not know how to specify the offset ( -0500 , -0200 , etc.). Here's the code to start the date string:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd HH':'mm':'ss"]; NSString *dateString = @"2013-01-25 00:00:00"; // <-------- Truncated ---- NSDate *date = [dateFormatter dateFromString:dateString]; NSLog(@"Date: %@", date); 

How can I make it work with the -0500 part? I tried SSSZ but it did not work (given null ).

+4
source share
1 answer

Try the following:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd HH':'mm':'ss ZZZ"]; NSString *dateString = @"2013-01-25 00:00:00 -0500"; NSDate *date = [dateFormatter dateFromString:dateString]; NSLog(@"Date: %@", date); 

I get:

Date: 2013-01-25 05:00:00 +0000

As an additional note, the standard for all date formats is used here: http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

+8
source

All Articles