Skip to main content

NSDateFormatter returning nil in OS 4.0



I had the following code working on on OS 3.x







NSString *stringDate = @"2010-06-21T20:06:36+00:00";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *theDate = [dateFormatter dateFromString:stringDate];

NSLog(@"%@",[dateFormatter stringFromDate:theDate]);







but now in the newest xcode 3.2.3 under the iOS4 simulator, the varialble theDate is nil.





I have looked through the class reference and do not see anything deprecated or implemented differently for iOS4 with these specific methods. What did i leave out?


Comments

  1. I found out it works if you do it this way (see below). The key is using the method:
    - [NSDateFormatter getObjectValue:forString:range:error:]

    instead of

    -[NSDateFormatter dateFromString]

    The complete code:

    + (NSDate *)parseRFC3339Date:(NSString *)dateString
    {
    NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
    [rfc3339TimestampFormatterWithTimeZone setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [rfc3339TimestampFormatterWithTimeZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

    NSDate *theDate = nil;
    NSError *error = nil;
    if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
    NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
    }

    [rfc3339TimestampFormatterWithTimeZone release];
    return theDate;
    }

    ReplyDelete
  2. Is your device set to 24 hour or 12 hour clock?

    That sounds like an insane question but I've just run into that bug - the dateformatter will adjust your format string according to the current locale which will include the time format settings.

    You can force it to ignore them by adding this line :

    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];


    Hope that helps.

    ReplyDelete
  3. This code will remove the extra colon as AtomRiot describes:


    Converting it from:


    NSString *stringDate = @"2010-06-21T20:06:36+00:00";


    to:


    NSString *stringDate = @"2010-06-21T20:06:36+0000";



    // Remove colon in timezone as iOS 4+ NSDateFormatter breaks
    if (stringDate.length > 20) {
    stringDate = [stringDate stringByReplacingOccurrencesOfString:@":"
    withString:@""
    options:0
    range:NSMakeRange(20, stringDate.length-20)];
    }


    for more details see: https://devforums.apple.com/thread/45837

    ReplyDelete
  4. The format that I am being given is halfway between RFC 822 and GMT. if i change the "+00:00" to "+0000" then I can use the "Z" on my format. and if i change it to "GMT+00:00" then I can use ZZZZ to get the data properly. It seems that something has been stripped out to handle this hybrid as it was working for me before with OS 3.x.

    ReplyDelete
  5. It seems like the Apple documentation is, well, 'tricky':


    The format string uses the format
    patterns from the Unicode standard
    (this reference is to version tr35-6
    for Mac OS X v10.5; for Mac OS X v10.4
    use version tr35-4).


    and


    iOS: The v10.0 compatibility mode is
    not available on iOS—only the 10.4
    mode is available.


    According to version tr35-4:


    Use 1 for GMT format, 2 for RFC 822

    Z{1} GMT-08:00
    Z{2} -0800


    But according to the Unicode standard:


    Use one to three letters for RFC 822, four letters for GMT format.

    Z{1,3} -0800
    Z{4} GMT-08:00


    So it looks like iOS 4 is using tr35-6 - the Unicode standard, which is why +00:00 now fails against 'Z'.

    I tried -08:00 against 'ZZZZ' in my NSDateFormatter and it failed in iOS 4. GMT-08:00, however, did work. It seems the timezone (GMT) is now required, rather than optional as it may have been in iOS 3.

    ReplyDelete
  6. Looks fine to me.

    Does it still function correctly on a real device?

    If so file a bug report for the simulator, otherwise file a bug report for iOS 4.

    ReplyDelete
  7. My previous answer is wrong. Sorry!
    I get the value from dataFormat @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'hh:mm";

    ReplyDelete
  8. I was just debugging this exact same problem. I have the same date string as you that works in 3.x and not 4.0. Same symptoms.

    When looking through the NSDateFormatter documentation I see:

    Initializing a Date Formatter
    – init Available in iPhone OS 2.0 through iPhone OS 3.2

    This says that the init method has been deprecated for iOS 4.0. I'm not sure what that means.

    ReplyDelete
  9. The link Elfred posted did the trick. I stumbled upon the same issue whilst converting my app from 3.1.3 to iOS4. The link holds the ISO8601DateFormatter class which is a far more excellent extension then my own date utility class.


    Elfred wrote: I ran into this issue recently. I ended up using Peter Hosey's ISO8601 parser. It is available here: http://boredzo.org/iso8601unparser/

    ReplyDelete
  10. It seems the NSDateFormatter has gotten very picky.

    -(void)dateFormatterTests {
    NSDateFormatter *formatter;

    formatter = [[NSDateFormatter alloc] init];

    #ifdef WORKS
    [formatter setDateFormat:@"yyyy-MM-dd"];
    #elif defined(ALSO_WORKS)
    [formatter setDateFormat:@"yyyy MM dd"];
    [formatter setLenient:YES];
    #else // DOESN'T WORK
    [formatter setDateFormat:@"yyyy MM dd"];
    #endif

    // Works per comments above
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13"]);
    // Never works with any of the above formats
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13 22:00"]);

    [formatter release]; formatter = nil;
    }

    ReplyDelete
  11. I had the same issue in my apps as well Null Value from NSDateFormatter.

    I found my problem to be the following:

    I was sending my app a date and time like this: 07/16/2010 04:21:00 +00:00 with the formatter like this: [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"]

    It seems like the ZZZ part of the formating NO LONGER accepts the colon : in the time.

    Works: 07/16/2010 04:21:00 +0000

    Doesn't work: 07/16/2010 04:21:00 +00:00

    To support the current apps that are out, all I did was search for the +00:00 part in the string and replace it with +0000.

    Hope this might help others.

    ReplyDelete
  12. I'm using it as simple as:

    date_formatter = [[NSDateFormatter alloc] init];
    [date_formatter setDateStyle:NSDateFormatterShortStyle];
    [date_formatter setTimeStyle:NSDateFormatterNoStyle];


    and this is working great. I don't understand how can they deprecate the init method, when the NSDateFormatter class is subclassed from NSObject

    ReplyDelete
  13. You use dataFormat @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z"

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex