If 语句比较两个日期

发布于 2025-01-07 14:30:32 字数 481 浏览 3 评论 0原文

如果文件超过 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
}

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2025-01-14 14:30:32
if ([date compare:fileModPlus24Hours] == NSOrderedDescending) {
    // update file
}

比较的结果可以是 NSOrderedAscendingNSOrderedDescendingNSOrderedSame

而且我认为通过将 datefileModPlus24Hours 进行比较,您的代码也不起作用。您应该将当前日期与 fileModPlus24Hours 进行比较:

NSDate *now = [NSDate date];
if ([now compare:fileModPlus24Hours] == NSOrderedDescending) {
    // update file
}
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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文