0

I have this code:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if (((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame))
                   && (compareEnd == NSOrderedDescending)) 

but it don't entry in the "if" when firstDate = dateSelected or secondDate = dateSelected...why??

Nicholas Knight
  • 15,774
  • 5
  • 45
  • 57
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

1 Answers1

2

Are you sure that when firstDate equals dateSelected that date selected is earlier than secondDate? Currently your logic is: firstDate <= dateSelected < secondDate. If you want firstDate <= dateSelected <= secondDate, then you need to implement the code as follows:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if ( (compareStart == NSOrderedAscending || compareStart == NSOrderedSame)
                   && (compareEnd == NSOrderedDescending || compareEnd == NSOrderedSame)) {
  // some code
}

Update:

To compare two NSDates without relying on time see: Comparing two NSDates and ignoring the time component

Community
  • 1
  • 1
adimitri
  • 1,296
  • 9
  • 13
  • you code entry in the "if" only when firstDate < dataSelected < secondDate; but I want that my code entry in the if when firstDate <= dataSelected <= secondDate – cyclingIsBetter Jun 06 '11 at 10:47
  • There must be some confusion. The code I supplied is: firstDate <= dateSelected <= secondDate – adimitri Jun 06 '11 at 10:53
  • 2011-06-06 12:59:48.994 Project[419:707] firstDate:2011-06-06 10:59:21 +0000 2011-06-06 12:59:49.001 Project[419:707] secondDate:2011-06-09 00:00:00 +0000 2011-06-06 12:59:49.004 Project[419:707] selectedData:2011-06-06 10:59:17 +0000 – cyclingIsBetter Jun 06 '11 at 11:02
  • selectedDate is 4 seconds before firstDate. That's why it's not working. – adimitri Jun 06 '11 at 11:11
  • I"m sorry, I don't understand what you mean. – adimitri Jun 06 '11 at 11:16
  • I've posted a link in the main post under "update" with the information you need. – adimitri Jun 06 '11 at 14:37