Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSDate isBetween

FontanoFontano Posts: 150Registered Users
edited February 2009 in iOS SDK Development
I did a few searches, but could not find a built in way to do a simple isBetween check for NSDates

So I wrote this up, and wanted to share in case anyone else was looking to do it.

(And also if someone knows of a different way)

NSDate_ext.h
@interface NSDate (Extend)
- (BOOL)isBetween:(NSDate *)dateStart:(NSDate *)dateEnd;
@end

NSDate_ext.m
@implementation NSDate (Extend)

- (BOOL)isBetween:(NSDate *)dateStart:(NSDate *)dateEnd {
	if ([self compare:dateEnd] == NSOrderedDescending)
		return NO;
	
	if ([self compare:dateStart] == NSOrderedAscending) 
		return NO;
	
	return YES;
}

@end

Then you can do a simple check on your date
NSDate *dateToCheck;
NSDate *dateRangeStart;
NSDate *dateRangeEnd;

BOOL isBetween = [dateToCheck isBetween:dateRangeStart :dateRangeEnd];

The code above return true if the dateToCheck IS EQUAL to either of the range ends.
Post edited by Fontano on

<br />
-) No matter what forum you join, you always start as a newbie. Even if you own the board.<br />
-) There is no stupid question, if you think it is stupid don't answer it and it will fall of the screen<br />
-) If you post some helpful sol

Replies

  • bgeerdesbgeerdes Posts: 99Registered Users
    edited February 2009
    Another way would be to convert the NSDates to NSTimeIntervals (via timeIntervalSinceReferenceDate), which are basically doubles, and compare those.
Sign In or Register to comment.