IOS: NSDate dateWithTimeIntervalSinceNow

IOS: NSDate dateWithTimeIntervalSinceNow

我如何使用 NSDate 的 dateTimeIntervalSinceNow 方法来计算今天和将来某个时间的 NSDate 之间的差异?

我看过的例子都是带参数的,比如秒数:

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];

我想在另一个 NSDate 上执行操作,如下所示:

pseudocode

NSDate *date = NSDate dateWithTimeIntervalSinceNow:(NSDate*)futureDate];

任何人都可以建议最好的方法吗?

谢谢

你的问题是乱码,不清楚

听起来您正在尝试计算两个日期之间的时间间隔。有一个方法:`timeIntervalSinceDate:'

如果您在变量 futureDate 中有未来日期,您的代码可能如下所示(在 Objective-C 中,因为您的伪代码看起来有点像 Objective-C。)

NSTimeInterval difference = [futureDate timeIntervalSinceDate: NSDate.date]

正如 Dan 在他的评论中所建议的那样,[futureDate timeIntervalSinceNow] 完成了任务。