4

I have a NSDateComponents problem. I have two NSDates that I am trying to compare by checking if their year, month and day match. This I am doing by converting the NSDate values to these integer components as follows:

//NSDate *cgiDate is previously set to 2011-08-04 00:00:00 +0000 
//NSDate *orderDate is previously set to 2011-08-04 14:49:02 +0000
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cgiDateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:cgiDate];
NSCalendar *orderCalendar = [NSCalendar currentCalendar];
NSDateComponents *orderDateComponents = [orderCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:orderDate];
if (([cgiDateComponents day] == [orderDateComponents day]) &&
    ([cgiDateComponents month] == [orderDateComponents month]) &&
    ([cgiDateComponents year] == [orderDateComponents year])) {
       GHTestLog(@"MATCHED");
 } else {
       GHTestLog(@"Not matched");
       GHTestLog(@"Day: %d vs. %d", [cgiDateComponents day], [orderDateComponents day]);
 }

My result is Not Matched, Day: 3 vs. 4. Why would this be?

I have read with great interest the following questions: NSDateComponents - day method returning wrong day and https://stackoverflow.com/questions/3920445/nsdatecomponents-incorrectly-reporting-day however neither answer my question of why this is not working.

Any advice?

Community
  • 1
  • 1
hocker
  • 688
  • 6
  • 18

3 Answers3

6

I think the issue is the following: Your dates are set in GMT time zone (+0000) If you are in the US, for example at GTM-6, then by the -currentCalendar the first date will be 6pm on Aug 3rd while second date will be 8:49am on Aug 4th.

You should force your calendar to have the UTC (GMT) timezone, or put the dates in your time zone, depending what is correct for your application.

Jacob Gorban
  • 1,431
  • 1
  • 9
  • 15
  • That's it... exactly the issue. The time zone doesn't really matter to me since I know both are in the same local time zone. – hocker Aug 04 '11 at 20:12
  • Setting the calendar to UTC is not necessarily the correct solution. It depends and what date you need. If you need the day in local time, then do not set the calendar's timezone. – rmaddy Nov 16 '16 at 22:23
2

It looks like you have a time zone issue. Though your dates are set at time zone +0000, your [NSCalendar calendar] call likely returns you a calendar for your local time zone. Given the adjustment to local time, the orderDate is likely in the next day. Manually set your calendars to time zone +0000.

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
1

NSDate has an isEqualToDate: function. so what you are trying to do can be done by:

[datea isEqualToDate:dateb];

there is also a Compare: function available, that returns the ordering of the dates.

edit: I'm now aware this doesn't answer your question about why the days return different values when they are set the same. Sorry! this still may help make your code a bit cleaner.

alexcash
  • 235
  • 2
  • 10