8

Possible Duplicate:
How to compare two dates in Objective-C

I'd like to pop up a message in one of my apps when the date is in the range of two dates (e.g. the holiday period)

So like

if(dateInRangeof:date1, date2){True}else{false}

Looking for any code snippets or apis to look at. Cheers

Community
  • 1
  • 1
Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
  • 1
    Check out this [question](http://stackoverflow.com/questions/949416/how-to-compare-two-dates-in-objective-c), I guess it'll be very helpful. – Mousa Nov 26 '11 at 04:18
  • 2
    I do not view this as duplicate. The answer below answers the specific question concisely. – wuf810 Apr 07 '13 at 10:43

1 Answers1

35
- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate {
   return [date compare:firstDate] == NSOrderedDescending &&
          [date compare:lastDate]  == NSOrderedAscending;
}
zaph
  • 111,848
  • 21
  • 189
  • 228
  • 8
    Just a reminder this is not inclusive. Inclusive would be `return !([date compare:firstDate] == NSOrderedAscending) && !([date compare:lastDate] == NSOrderedDescending;)` – nagem Jun 19 '14 at 18:27
  • 4
    Swift: ```func isDate (date : NSDate, inRange fromDate : NSDate, toDate : NSDate, inclusive : Bool) -> Bool { if inclusive { return !(date.compare (fromDate) == .OrderedAscending) && !(date.compare (toDate) == .OrderedDescending) } else { return date.compare (fromDate) == .OrderedDescending && date.compare (toDate) == .OrderedAscending } } ``` – John Slade Jul 17 '15 at 23:42