[英]If statement to compare 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];
考慮類似的東西:
if (date > fileModPlus24Hours) {
// update file
}
有什么建議么?
if ([date compare:fileModPlus24Hours] == NSOrderedDescending) {
// update file
}
比較的結果可以是NSOrderedAscending
, NSOrderedDescending
或NSOrderedSame
。
而且我認為通過將date
與fileModPlus24Hours
進行比較,您的代碼也不起作用。 您應該將當前日期與fileModPlus24Hours
進行比較:
NSDate *now = [NSDate date];
if ([now compare:fileModPlus24Hours] == NSOrderedDescending) {
// update file
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.