views:

961

answers:

4

Hi all,

I have an NSTimeInterval, and I'd like to know whether that timestamp falls between the start and end of a date. Basically I need to be able to do something like:

NSDate *today = [NSDate date];
NSTimeInterval myInterval = someInterval;

[date returnYesIfFallsThisDate:myInterval];

Is there a straight forward method to figure out if my NSTimeInterval falls on the same day as an NSDate object?

Thanks all.

+1  A: 

Conceptually you want two dates that constitute the "start" and "end" of the target date in question. Then you can test the time interval directly against the respective time intervals of those dates.

For instance, if I live in San Francisco, then I want the "start" to be 12AM pacific time, and the end to be 11:59:59.99999 PM pacific time.

Once you've figured out what time zones/etc you want to consider as the start and end points, you can just use the time intervals to do the test:

if ((myInterval >= [earlyMorningDate timeIntervalSinceReferenceDate]) && 
    (myInterval <= [lateEveningDate timeIntervalSinceReferenceDate]))
{
    NSLog(@"Whoo hoo - it's the right date!");
}
danielpunkass
12 PM is noon. Perhaps you mean 11:59:59.99999…?
Peter Hosey
Thanks, Peter. Yes ... edited.
danielpunkass
+1  A: 

Keep in mind that NSDate doesn't represent a "day" in any form, just a point in time. You'll have to do a little work with NSDateComponents, converted using an NSCalendar (typically the user's default calendar) to figure out the start and end NSDate values, and compare use those to compare your time interval to.

I would start by taking the NSDate that falls within your day, and converting it to an NSDateComponents, but without hours, minutes or seconds. Here's a quick (untested) example to help you get started:

NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit) fromDate:[NSDate date]];
NSDateComponents *day = [[[NSDateComponents alloc] init] autorelease];
[day setDay:1];
NSDate *start = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSDate *end = [[NSCalendar currentCalendar] dateByAddingComponents:day toDate:start options:0];

Then you can compare the time interval as suggested by Mr. Jalkut (or another method, NSDate can work with NSTimeIntervals in a few different ways). I would definitely spend some time looking over NSCalendar and NSDateComponents in the docs though, you might find a better method than what I'm suggesting for what you need to do.

Marc Charbonneau
+1  A: 

An NSTimeInterval is just a number of seconds. To compare it to an NSDate, you need to interpret the time interval relative to some reference point, then create an NSDate with the result, then compare the two NSDates.

There are two standard reference dates: 2001-01-01 and 1970-01-01 (the latter being “UNIX time”). If neither of those is the correct reference date, use NSCalendar to create an NSDate for the correct reference date, then create your NSDate of interest relative to that.

Now for the comparison.

If all you care about is the date on the calendar, then you'll create the NSDate, then use an NSCalendar to get NSDateComponents for the date. Specifically, you'll get the year, month, and day-of-the-month, then compare those.

There's no need to worry about “start” and “end” times; in fact, if you only care about the date on the calendar, you can ignore the time-of-day entirely.

Peter Hosey
Peter's point seems valid. I will withdraw my answer if people agree that it's better to use NSCalendar to do this kind of date comparison, instead of trying to interpret it on ones own as I suggested.
danielpunkass
Hey Peter, your post is very helpful. Thanks again for sharing your wisdom. Daniel, your post was helpful as well for other issues I'm dealing with that are time sensitive, so thanks. Peter's post directly addresses the topic at hand. Thanks folks.
A: 

Is it just me or does the OP seem to think that a NSTimeInterval is a representation of an interval in time from A to B? I.e. an absolute distance (in seconds) from Date A to Date B.

The original question needs to be repose to state that you need to find it based on the reference date. Something like this:

NSDate *refDate = someReferenceDate;
NSTimeInterval interval = someInterval;
NSDate *today = [NSDate date];
BOOL isInRange = false;

isInRange = [today isInterval:interval inRangeFromReference:refDate];

Then that method would use some (or parts of all) of the startegies that Damiel, Peter and Marc mention above.

littleknown