我想检查一下我拥有的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
知道它为什么不起作用?谢谢!
答案 0 :(得分:9)
NSDate *now = [NSDate date];
if ([now compare:muteOverDate] == NSOrderedAscending) {
// muteOverDate in the past
}