如果在过去 24 小时内未检查代码,则需要检查代码

Need to check a code if it is not checked in last 24 hours

我每天只需要检查一次代码块,我尝试了下面的代码,但它没有按照我的要求工作。就像今天的日期是 25-5-2017,最后检查的日期是 23-5-2017,它也不在条件范围内。我不知道我哪里做错了。

            NSDate *todayDate = [NSDate date];
            NSLog(@"today date is %@",todayDate);
            NSDate *lastCheckedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastCheckedDate"];
            NSLog(@"lastCheckedDate is %@",lastCheckedDate);
            if ([todayDate compare:lastCheckedDate] > 864000.0f) {
                NSArray *appMsgsArray = [response valueForKey:@"AppMsgs"];
                NSDictionary *mainDict = [appMsgsArray objectAtIndex:0];
                NSString *appDataExpiry = [mainDict valueForKey:@"appdata_expiry"];
                [stm deleteOldArticles:[appDataExpiry integerValue]];
                NSDate *lastDate = [NSDate date];
                [[NSUserDefaults standardUserDefaults] setObject:lastDate forKey:@"LastCheckedDate"];
            }

有一天我使用了 864000.0f 值。谁能帮我解决这个问题,谢谢。

试试这个:

NSDate *firstDate = [NSDate date];
NSDate *secondDate = [NSDate date];

if ([secondDate timeIntervalSinceDate:firstDate] >= 86400) {
    NSLog(@"24 hours have passed...");
}