检查NSDate对象是否在过去

时间:2016-02-10 15:03:44

标签: ios objective-c cocoa-touch nsdate

我想检查一下我拥有的NSDate对象是在过去还是现在(如果它不在将来)。

我试过这个,但它不起作用:

-(void)checkIfMuted
{
    NSDate *muteOverDate=[[NSUserDefaults standardUserDefaults] objectForKey:@"muteOverDate"];

    if ([muteOverDate timeIntervalSinceNow]<0.0) {
        self.muteEnabled=YES;
        return;
    }
    self.muteEnabled=NO;
    NSLog(@"%@\n%@",[[NSDate date] description],[muteOverDate description]);
}

NSLog打印: 2016-02-10 14:55:19 +0000 2016-02-10 15:54:32 +0000

知道它为什么不起作用?谢谢!

1 个答案:

答案 0 :(得分:9)

使用[NSDate compare:]

NSDate *now = [NSDate date];
if ([now compare:muteOverDate] == NSOrderedAscending) {
    // muteOverDate in the past
}