1

The following code:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSDate *tempDate = [dF2 dateFromString:@"2011-07-10"];
DebugLog(@"Temp Date %@", tempDate);

Results in the following output:

Temp Date 2011-07-10 05:00:00 +0000

I don't understand where the 05:00:00 is coming from? I am using this code in the Central timezone, which is -5 GMT and that's the only connection I can think of to the time that set for the date. I expected 00:00:00.

Input appreciated.

purplehey
  • 127
  • 1
  • 12

1 Answers1

3

When you created the NSDate 07-10-2011 the NSDataFormatter defaults to current timezone. Which in your example is GMT-5.

Remember also, the debugger will display NSDate in UTC/GMT+0;

If you want to create UTC midnight:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd";
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *tempDate = [dateFormatter dateFromString:@"2011-07-10"];
NSLog(@"Temp Date %@", tempDate);
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • Wow...I had no idea that the debugger displays NSDate in UTC/GMT+0. I find the NSDate interface pretty confusing I must say. Thanks much @black-frog! – purplehey Apr 19 '11 at 20:37