7

I'm having a problem with the NSDateFormatter. It is returning null for some specific dates.

I did this function to debug the case:

- (void) debugTest:(NSString *)dateStr{

    NSLog(@"String: %@", dateStr);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];

    NSDate *dateObj = [df dateFromString: dateStr];

    NSLog(@"Date: %@", dateObj);

}

This is my log output for some dates:

String: 2012-10-18
Date: 2012-10-18 03:00:00 +0000
String: 2012-10-19
Date: 2012-10-19 03:00:00 +0000
String: 2012-10-20
Date: 2012-10-20 03:00:00 +0000
String: 2012-10-21
Date: (null)
String: 2012-10-22
Date: 2012-10-22 02:00:00 +0000
String: 2012-10-23
Date: 2012-10-23 02:00:00 +0000

Why it is returning null only for the date 2012-10-21?

I did other tests with the interval from 2011-10-01 to 2014-11-01 and the following dates returned null:

2011-10-16, 2012-10-21, 2013-10-20, 2014-10-19

Is this some kind of bug? I am doing something wrong?

adarshr
  • 61,315
  • 23
  • 138
  • 167
Rafael
  • 1,655
  • 3
  • 17
  • 25
  • Related? http://stackoverflow.com/a/4265130/343955 – adarshr Oct 01 '12 at 12:37
  • I tried the solution of the other question but didn't worked for me. I think that the problem is different. – Rafael Oct 01 '12 at 12:52
  • Code works fine. It's something outside of the list. I expect that string has gone out of scope. Please post rest of testing code. – madmik3 Oct 01 '12 at 12:53
  • 1
    That function is the only code that I am testing. I got it to work now. After looking adarshrs's related question, I added to my date formatter [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; AND [df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];. I think the problem was related to the device's configuration, setting the two properties fixed the problem. – Rafael Oct 01 '12 at 12:59
  • what is the frequency to call the `-debugTest:` method? if you run the `-debugTest:` method with this string `2012-10-21` only, what do you get back? – holex Oct 01 '12 at 13:07
  • I was calling it for each day of the time interval that I wanted. But the problem is solved now. Thanks for your time! – Rafael Oct 01 '12 at 13:17

1 Answers1

5

I will answer my own question just to register in case of someone get the same problem:

I added to my date formatter [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; AND [df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];.

So, my new code is:

- (void) debugTest:(NSString *)dateStr{

    NSLog(@"String: %@", dateStr);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [df setDateFormat:@"yyyy-MM-dd"];

    NSDate *dateObj = [df dateFromString: dateStr];

    NSLog(@"Date: %@", dateObj);

}

I think the problem was related to the device's configuration, setting the two properties fixed the problem.

Rafael
  • 1,655
  • 3
  • 17
  • 25
  • It didn't work for me. I am still getting partial results. Some null values and some date string. How you were able to make it work? – AJ112 Sep 18 '13 at 11:00
  • I did exactly what i posted and worked for me. But i did this a long time ago, so i don't know if in the most recent iOS versions this still works, maybe something changed. – Rafael Sep 18 '13 at 12:16