My NSDateFormatter only works in iPhone simulator

I use NSDateFormatter, which works fine in the simulator, but I get zero when I run it on the iPhone. I hard-coded the date to be sure of the format, but that is all the same.

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

I tried with various region settings, languages, etc. on the iPhone. Any idea what is going wrong?

+2
source share
7 answers

This code works correctly with the tag "GMT".

 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]; 

The RFC822 date and time specifications use multiple “zone” tags, and I found the correct character to parse the “GMT” tag. String format for iPhone NSDateFormatter

  • z ~ zzz: (Specific GMT time zone abbreviation)
  • zzzz: (The specific name is GMT time zone)
  • Z: +0000 (RFC 822 Time Zone

Thanks for your help!

+9
source

If you need to use a specific date format, you can parse it manually, rather than using NSDateFormatter . Its behavior changes depending on the locale, etc., and there are some errors, especially if you have a time zone in your line.

Having said that, one of the options for finding the problem may be to use the getObjectValue:forString:range:error: dateFromString: instead of dateFromString: This way you get an NSError object, which (theoretically) will tell you what the problem is.

By the way, you do not need the string NSDateFormatterBehavior10_4 . iPhone OS only supports 10.4+ parameters, although you will not get any errors if you use the "old" style in the simulator.

+2
source

I had this problem recently (iOS5). I also needed to install the NSDateFormatter language to work with the device. Without this, [dateFormatServer dateFromString:dateServer] returned 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]; 
+2
source

Nothing stands out, but to help debug this, you can try to file an NSDateFormatter date object and see if there are any slight differences in the resulting line from the one you are trying to parse.

+1
source

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

+1
source

Manuel Spuhler’s second manual analysis tip is not my favorite option, but the Objective-C options are too complicated for this (and there aren’t enough error messages - something isn’t so easy to spit nil without an error message).

One thing that worked for me was to use C strptime to separate the date and then restore it as an NSDate object. For example, the code below transfers the string received as “Monday, 28-Sep-09 18:13:50 UTC” and converts it to an NSDate object, adapting the UTC time to 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] ]; 

(can handle other zones by adding other parameters to struct tm instead of the fixed time zone +0000, see time.h entry in wikipedia for more information):

0
source

strptime saved me too.

 struct tm time; if (strptime([modDateString UTF8String], "%A, %d %b %Y %H:%M:%S", &time)) { NSString *rDate=[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]; 

This code works with HTTP dates like Thu, Apr 15, 2010 1:53:56 PM GMT and Fri, May 8, 2009 08:08:35 GMT, which was the original question

0
source

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


All Articles