如果声明比较两个日期

时间:2012-02-20 20:02:44

标签: iphone objective-c cocoa nsdate

如果文件超过24小时,我试图自动更新plist文件,但我无法弄清楚如何比较这两个日期。

// get last modification date
NSString *dir = path;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:dir error:nil];
NSDate *date = [attributes fileModificationDate];

NSDate *fileModPlus24Hours = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:date];

思考类似的事情:

if (date > fileModPlus24Hours) {
    // update file
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

if ([date compare:fileModPlus24Hours] == NSOrderedDescending) {
    // update file
}

比较结果可以是NSOrderedAscendingNSOrderedDescendingNSOrderedSame

我不认为您的代码可以通过将datefileModPlus24Hours进行比较来实现。您应该将当前日期与fileModPlus24Hours进行比较:

NSDate *now = [NSDate date];
if ([now compare:fileModPlus24Hours] == NSOrderedDescending) {
    // update file
}