0

I'm trying to create NSDate containing only today's date, not time. And I don't need any timezone support. Basically just get today's date from the current calendar.

NSCalendar* calendar = [NSCalendar currentCalendar];
// [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents* components = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                           fromDate: [NSDate date]];
NSDate* today = [calendar dateFromComponents: components];

If I uncomment commented line I get what I need, but if not - I'm getting NSDate object pointing to 7am today's date (I'm in Pacific Time Zone).

Why?

sha
  • 17,824
  • 5
  • 63
  • 98
  • It is just the other way around: If you don't set the time zone to GMT then you get 00:00 of the current day in your time zone. But the `description` method of NSDate displays that as 07:00 GMT (as explained in the "possible duplicate"). – Martin R Apr 16 '14 at 17:37
  • Think about it like this, perhaps: if you want the date created from the components to be in reference to a particular time zone, you must set the time zone _of the components_, just as you would set the value of any other element you cared about. – jscs Apr 16 '14 at 18:27

0 Answers0