creating a wrong NSDate

40 views Asked by At

This is the code

  NSDateComponents *comps = [[NSDateComponents alloc] init];
  [comps setDay:13];
  [comps setMonth:6];
  [comps setYear:2015];

  NSCalendar *gregorian = [[NSCalendar alloc]
                           initWithCalendarIdentifier:NSGregorianCalendar];
  NSDate *date = [gregorian dateFromComponents:comps];

NSLog(@"date = %@", date);

the result is 2015-06-12 23:00:00 +0000

12? the day is 13!!!

Why is that happening? Bug?

1

There are 1 answers

6
zaph On BEST ANSWER

The problem is with the way NSLog() and the NSDate description method display the date which is in GMT (UTC). Notice the time offset: "+0000" which is probably not your timezone.

All dates are stored internally in GMT. To obtain a string in the current or another time zone use NSDateFormatter.