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

后端 未结 4 1621
囚心锁ツ
囚心锁ツ 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!");
    }
    

提交回复
热议问题