簡體   English   中英

如果語句比較兩個日期

[英]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
}

比較的結果可以是NSOrderedAscendingNSOrderedDescendingNSOrderedSame

而且我認為通過將datefileModPlus24Hours進行比較,您的代碼也不起作用。 您應該將當前日期與fileModPlus24Hours進行比較:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM