34

I am trying to create a formatter that will convert the date format shown to an NSDate object:

NSString *dateStr = @"2010-06-21T19:00:00-05:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];  

The issue is the timezone -05:00, which is not parsed properly with the format above. Any suggestions?

John Muchow
  • 4,798
  • 8
  • 40
  • 40
  • 1
    does your pattern really match the input? – willcodejavaforfood Jun 22 '10 at 15:45
  • 1
    The format is all good up to the timezone (-05:00). If I remove the ':' the formatter works. Trying to understand if there is a means to have a formatter that will properly parse the timezone as is. – John Muchow Jun 22 '10 at 16:06
  • See my answer here: http://stackoverflow.com/questions/3094819/nsdateformatter-returning-nil-in-os-4-0/3968411#3968411 – Werner Altewischer Dec 07 '12 at 14:48
  • pueden ayudarme con estan pregunta http://stackoverflow.com/questions/15162835/how-to-use-nsdateformatter-to-venezuela gracias – tony Mar 01 '13 at 17:26
  • User `NSISO8601DateFormatter` instead as suggested by [Apple doc](https://developer.apple.com/documentation/foundation/nsiso8601dateformatter) – DawnSong Jun 27 '19 at 17:33

6 Answers6

64

To process the time zone with the colon in it, you just need to use 5 'Z's. This is a pretty common date format, the ISO-8601 format. This will only work on iOS 6.x+

-(NSDate *) dateFromString:(NSString *)string {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

    return [formatter dateFromString:string];
}
brynbodayle
  • 6,546
  • 2
  • 33
  • 49
31

Honestly, you'll just have to change the source data (removing the colon) before running it through the formatter. Your original date string is non-standard and none of the time zone format strings will work properly on it.

You can see the valid inputs on unicode.org.

ZZZ e.g. "-0500"

ZZZZ e.g. "GMT-05:00"

Nothing for "-05:00"

chrissr
  • 9,861
  • 2
  • 29
  • 30
  • After trying any number of approaches, that is what I settled on as well. Thanks – John Muchow Jun 22 '10 at 21:00
  • I know this is an old question but I wanted to comment that I faced the exact same formatting issue. That format is the ISO 8601 format, which is a pretty standard format. – Chris C Aug 21 '12 at 00:22
  • 1
    thank you for the answer to the extra : in the timezone. I was getting a timezone from a google rss feed that had -04:00 and it was driving me crazy for hours. Your solution to change the tz to -0400 was the trick. – jangelo42 Sep 21 '12 at 00:51
  • 3
    This is not the most optimal solution anymore. See my answer below. – brynbodayle Dec 18 '12 at 23:02
  • Note: Use lowercase 'z' to get just the timezone string (e.g. `zzz` -> `EDT`) – Albert Renshaw Oct 19 '20 at 00:25
3

May be I missed something but ZZ worked for me. I used:

@"yyyy-MM-dd'T'HH:mm:ss.SSSZZ"

for

2014-02-27T08:00:00.000+04:00
DanSkeel
  • 3,853
  • 35
  • 54
1

This is the default time format I got from a Sinatra ActiveRecord backend. Here is my solution.

-(NSDate *) dateFromString:(NSString *)string{
    NSMutableString * correctedDateString = [[NSMutableString alloc] initWithString:string];
    [correctedDateString deleteCharactersInRange: NSMakeRange(22, 1)];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];

    return [formatter dateFromString:correctedDateString];
}
Logan
  • 349
  • 2
  • 3
1

This is the only solution that worked for me:

[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
Dave Cole
  • 2,446
  • 2
  • 20
  • 26
0

5 ZZZZZ - heres a category I wrote with some sample of GMT to BST

https://github.com/clearbrian/NSDateFormatter_ISO_8601

brian.clear
  • 5,277
  • 2
  • 41
  • 62