How to determine if an NSTimeInterval occurred during an arbitrary NSDate?

后端 未结 4 1622
囚心锁ツ
囚心锁ツ 2021-01-07 15:10

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:



        
相关标签:
4条回答
  • 2021-01-07 15:46

    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!");
    }
    
    0 讨论(0)
  • 2021-01-07 15:58

    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.

    0 讨论(0)
  • 2021-01-07 15:59

    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.

    0 讨论(0)
  • 2021-01-07 16:06

    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.

    0 讨论(0)
提交回复
热议问题