Question

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"]]);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top