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

后端 未结 4 1776
囚心锁ツ
囚心锁ツ 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: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.

提交回复
热议问题