I have an NSDateComponents problem. I have two NSDates that I am trying to compare by checking if their year, month and day match. I do this by converting 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 does not match, day: 3 against 4. Why should it be?
I read with great interest the following questions: NSDateComponents - the method of the day that returns the wrong day and https://stackoverflow.com/questions/3920445/nsdatecomponents-incorrectly-reporting-day however do not answer my question why this does not work.
Any tips?
source share