0

I have an array with 900+ string objects...

When i format this strings into dates, there is 1 date that can't be formatted, giving me a null value...

i've tested in another blank Xcode project and it was bugged too..

Can anyone pls check if this is an error from my machine or Xcode ? The date is 02/11/2004 (dd/mm/yyyy).

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];    
NSLog([NSString stringWithFormat:@"%@", [formatter dateFromString:@"02/11/2004"]]);
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
brbgyn
  • 411
  • 1
  • 3
  • 12
  • And this has what to do with Xcode exactly? – Léo Natan Feb 22 '14 at 04:29
  • I wonder if this has something to do with locale or time zones. What is your locale and time zone? – Dietrich Epp Feb 22 '14 at 04:37
  • This is odd, I just tried this on mine and I'm not getting a null value. Can you try printing out the date with this line of code and let me know if you still see a null value? `NSLog(@"%@", [[formatter dateFromString:@"02/11/2004"] descriptionWithLocale:[NSLocale currentLocale]]);` – ajfigueroa Feb 22 '14 at 05:33
  • There must be something more to your code that causes the bug. The snippet you're showing works just fine. – Jay Feb 22 '14 at 07:02
  • @calzone21, still null :( – brbgyn Feb 22 '14 at 13:03

1 Answers1

1

You are converting dates without specifying a time zone, so your local one is assumed. After a little research at a guess you're in Brazil in the time zone of Sao Paulo, Campo Grande or Cuiba - Nov 2 2004 is when daylight savings changed in those locations and 0000 didn't exist as time jumped to 0100.

If you are just after the date conversion you can set the time zone to UTC (which has no daylight savings), add:

formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

and you won't get null for that date.

CRD
  • 52,522
  • 5
  • 70
  • 86