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

时间:2017-05-25 07:09:37

标签: ios objective-c nsdate

我需要每天只检查一个代码块,我尝试了下面的代码,但它不符合我的要求。像今天的日期是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值。谁能帮助我,谢谢。

1 个答案:

答案 0 :(得分:2)

试试这个:

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

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