简体   繁体   中英

If statement to compare two dates

Im trying to update a plist file automaticlly if the file is over 24 hours old, but i cant figure out how to compare the two dates.

// 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];

Thinking about something like:

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

Any suggestions?

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

The result of compare can be NSOrderedAscending , NSOrderedDescending or NSOrderedSame .

And I don't think your code will work either by comparing date to fileModPlus24Hours . You should be comparing the current date to fileModPlus24Hours instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM