[英]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. 我正在尝试自动更新plist文件(如果文件已超过24小时),但是我不知道如何比较两个日期。
// 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
. 比较的结果可以是
NSOrderedAscending
, NSOrderedDescending
或NSOrderedSame
。
And I don't think your code will work either by comparing date
to fileModPlus24Hours
. 而且我认为通过将
date
与fileModPlus24Hours
进行比较,您的代码也不起作用。 You should be comparing the current date to fileModPlus24Hours
instead: 您应该将当前日期与
fileModPlus24Hours
进行比较:
NSDate *now = [NSDate date];
if ([now compare:fileModPlus24Hours] == NSOrderedDescending) {
// update file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.