我有NSDate
一个持续时间。我需要在持续时间之后得到时间
给定:
日期为“2010-02-24 12:30:00 -1000”
持续时间为 3600 秒
我需要得到“2010-02-24 13:30:00 -1000”
我想dateWithTimeIntervalSinceReferenceDate:
,会做到这一点,但我现在看到这给出了从 2001 年 1 月 1 日格林威治标准时间开始的日期偏移量。
我需要使用另一个 C 函数吗
我有NSDate
一个持续时间。我需要在持续时间之后得到时间
给定:
日期为“2010-02-24 12:30:00 -1000”
持续时间为 3600 秒
我需要得到“2010-02-24 13:30:00 -1000”
我想dateWithTimeIntervalSinceReferenceDate:
,会做到这一点,但我现在看到这给出了从 2001 年 1 月 1 日格林威治标准时间开始的日期偏移量。
我需要使用另一个 C 函数吗
正如 DyingCactus 所说,您可以使用 NSDate 的 addTimeInterval 方法,但根据操作系统版本会产生编译器警告,因为它在 2009 年 8 月 17 日的 10.6 中已弃用。
当前推荐的方法是在 NSDate 上使用 dateByAddingTimeInterval:
NSDate *newDate = [oldDate dateByAddingTimeInterval:3600];
请注意,如果您的目标是 10.5 或更早版本,则原始答案将起作用。
You want the class method dateWithTimeInterval:sinceDate:
which takes the starting date and the interval NSDate docs
可以使用 NSDate 的 addTimeInterval 实例方法:
NSDate *newDate = [oldDate addTimeInterval:3600];
编辑:正如 Chip Coons 正确指出的那样,此方法已被弃用。请改用其他方法之一。
对于那些寻找 Swift 3.x 版本的人:
let newDate = Date.init(timeInterval: 2600, since: oldDate)
您可以使用 NSDate 的较新的 dateByAddingTimeInterval 实例方法,而不是使用已弃用的 addTimeInterval 方法。
NSDate *newDate = [oldDate dateByAddingTimeInterval:2500];