I have the following NSDateFormatter:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM. d, yyyy"];
NSLocale *usLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocal];
and I have a while loop which starts at a given start date and increments the date by one until it reaches a specific end date.
NSDate *d = start;
while(YES){
NSString *td = [dateFormatter stringFromDate:d];
NSLog(@"converted date %@ to %@",d,td);
...adds a date and re-initializes d
}
And I'm getting output such as the following:
2011-06-11 17:10:18.678 ListOf100[8784:707] converted date 2011-05-31 00:00:00 +0000 to May. 30, 2011
2011-06-11 17:10:18.687 ListOf100[8784:707] converted date 2011-06-01 00:00:00 +0000 to May. 31, 2011
2011-06-11 17:10:18.717 ListOf100[8784:707] converted date 2011-06-02 00:00:00 +0000 to Jun. 1, 2011
As you can see, all the dates are not converting properly. They are all off by 1 day. Why could this be happening? And how can I fix it?
Looks to me like the dates are being created in a different time zone than the one the formatter's working in.
Yields:
Don't know why it's happening, but if it's persistent, fixing it shouldn't be harder than adding a day to d before converting it: