Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate between two given NSDates

Tags:

cocoa

nsdate

I am interested in creating a method to find if the current date falls between certain times on any given day. It is for a scheduling program, so I want to find what event is occurring at the current time. Every day has the same set of times: 820-900, 900-940, 940-1020, etc. Since this must be done on any given day I do not know how to create an NSDate with a certain time. I think this might be done with NSTimeInterval but I am not sure how to instantiate that.

like image 645
azban Avatar asked Dec 31 '09 23:12

azban


3 Answers

This isn't perfect, but you could use [NSDate compare:] to check your date against both boundaries:

NSDate *firstDate = ...
NSDate *secondDate = ...

NSDate *myDate = [NSDate date];

switch ([myDate compare:firstDate]) {
    case NSOrderedAscending:
        NSLog(@"myDate is older");
        // do something
        break;
    case NSOrderedSame:
        NSLog(@"myDate is the same as firstDate");
        // do something
        break;
    case NSOrderedDescending:
        NSLog(@"myDate is more recent");
        // do something
        break;
}

switch ([myDate compare:secondDate]) {
    case NSOrderedAscending:
        NSLog(@"myDate is older");
        // do something
        break;
    case NSOrderedSame:
        NSLog(@"myDate is the same as secondDate");
        // do something
        break;
    case NSOrderedDescending:
        NSLog(@"myDate is more recent");
        // do something
        break;
}

Or more briefly:

BOOL between = NO;

if (([myDate compare:firstDate] == NSOrderedDescending) &&
    ([myDate compare:secondDate] == NSOrderedAscending)) {

    between = YES;
}

I'm sure there's a better way to do complex date comparison, but this should work.

like image 125
conmulligan Avatar answered Oct 20 '22 10:10

conmulligan


The easiest way to do this would be to use -timeIntervalSinceReferenceDate to turn each of your dates into an NSTimeInterval typed value, which is really just a double.

NSTimeInterval rightNow = [[NSDate date] timeIntervalSinceReferenceDate];

From there, determining if a date is in between any given two dates is just a matter of simple numeric comparisons.

If you need to convert from a string representation of a date to an NSDate instance to then retrieve a time interval, use NSDateFormatter.

If you need to create a date from known date components, use NSCalendar. (i.e. you know the year is 2010, the month is 4 and the day is 12, you can use NSCalendar's components to generate an NSDate instance via the -dateFromComponents: method.).

As Ben indicated, NSCalendar's component interface can also be used to suss out the hour & minute to determine if it is in a range (which would seemingly be an atypical usage in that most events don't happen every day at the same time... but... sure... there are reasons to do that, too!)

like image 44
bbum Avatar answered Oct 20 '22 10:10

bbum


Take a look at -[NSCalendar components:fromDate:]. It will let you 'decompose' a date into its various components, and then you can, in this instance, look at the hour and minute.

like image 4
Ben Gottlieb Avatar answered Oct 20 '22 11:10

Ben Gottlieb