my NSDateFormatter works only in the iPhone simulator

后端 未结 7 2052
独厮守ぢ
独厮守ぢ 2020-12-20 04:13

I use a NSDateFormatter which works fine in the simulator, but I get a nil when I run it in the iPhone. I hardcoded the date to be sure of the format, but it fails anyway. <

相关标签:
7条回答
  • 2020-12-20 04:20

    Nothing stands out, but to help debug this you could try feeding NSDateFormatter a date object and seeing if the resulting string has any minor differences from the one you're trying to parse.

    0 讨论(0)
  • 2020-12-20 04:22

    When I've parsed a date string with "GMT" at the end, I've used the "zzz" format, not "Z".

    0 讨论(0)
  • 2020-12-20 04:23

    I had also this problem recently (iOS5). I needed to set also the locale of the NSDateFormatter to work on the device. Without this, [dateFormatServer dateFromString:dateServer] was returning null.

    NSString *dateServer = @"Fri, 8 May 2009 08:08:35 GMT";
    
    NSDateFormatter *dateFormatServer = [[NSDateFormatter alloc] init];
    [dateFormatServer setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"];
    
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
    [dateFormatServer setLocale:locale];
    [locale release];
    
    NSDate * date = [dateFormatServer dateFromString:dateServer];
    
    0 讨论(0)
  • 2020-12-20 04:28

    If you need to use a specific date format you might want to parse it "by hand" rather than using NSDateFormatter. Its behaviour does change depending on the locale, etc. and there are some bugs particularly when you have a timezone in your string.

    Having said that, one option in finding what the problem is might be to use the getObjectValue:forString:range:error: method instead of dateFromString:. This way you get an NSError object that (in theory) would tell you what the problem is.

    BTW, you don't need the NSDateFormatterBehavior10_4 line. iPhone OS only supports the 10.4+ options, though you won't get any errors if you use the "old" style in the Simulator.

    0 讨论(0)
  • 2020-12-20 04:30

    This code seems to work correctly against the "GMT" tag.

    NSString *strPubDate = @"Fri, 8 May 2009 08:08:35 GMT";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"];
    NSDate *myDate = [dateFormatter dateFromString:strPubDate];
    

    RFC822 Date and Time specs use a few "zone" tags and I found the correct symbol to parse the "GMT" tag Format String for the iPhone NSDateFormatter

    • z~zzz: (Specific GMT Timezone Abbreviation)
    • zzzz: (Specific GMT Timezone Name)
    • Z: +0000 (RFC 822 Timezone

    Thanks for all your help!

    0 讨论(0)
  • 2020-12-20 04:30

    I second Manuel Spuhler's advice of manually parsing - not my favorite option, but Objective-C's options for that are way too complicated (and lacking in error reporting - anything wrong just spits nil, without a hint on the error).

    One thing that worked for me is to use C's strptime to decouple the date, then reconstruct it as a NSDate object. For example, the code below tkes a string received as something like "Monday, 28-Sep-09 18:13:50 UTC" and converts it to a NSDate object adapting the UTC time for the local time:

        struct tm time;
        strptime([currentStringValue UTF8String], "%A, %d-%b-%y %H:%M:%S %z", &time);   
        NSDate* myDate = [NSDate dateWithString:
            [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d +0000",
                time.tm_year+1900, time.tm_mon+1, time.tm_mday,
                time.tm_hour, time.tm_min, time.tm_sec]
        ];
    

    (could handle other zones by adding other struct tm parameters instead of the +0000 fixed time zone, see time.h entry on wikipedia for details):

    0 讨论(0)
提交回复
热议问题