gpt4 book ai didi

ios - timeIntervalSinceReferenceDate 与 timeIntervalSince1970 与 timeIntervalSinceNow

转载 作者:IT王子 更新时间:2023-10-29 05:45:23 27 4
gpt4 key购买 nike

我正在尝试研究 Swift 2 中的 NSDate 类。

我发现每个NSDate对象都有三个属性,分别是:

  1. timeIntervalSinceReferenceDate
  2. timeIntervalSince1970
  3. timeIntervalSinceNow

让我有点困惑。虽然我知道它们代表秒,但我无法理解它们的解释。

我做了这段代码:

let whatISThis = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60))

好吧,我有一个约会,但那是什么?这三个属性有什么区别?

最佳答案

timeIntervalSinceReferenceDate 是自 2001 年 1 月 1 日凌晨 12:00(午夜)以来的秒数

timeIntervalSince1970 是自 1970 年 1 月 1 日凌晨 12:00(午夜)以来的秒数

timeIntervalSinceNow 是从现在开始的秒数

我将列举这些例子:

let s0 = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(0)) // it means give me the time that happens after January,1st, 2001, 12:00 am by zero seconds
print("\(s0)") //2001-01-01 00:00:00

let s60 = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60)) //it means give me the time that happens after January, 1st, 2001, 12:00 am by **60 seconds**
print("\(s60)") //2001-01-01 00:01:00

let s2 = NSDate(timeIntervalSince1970: NSTimeInterval(0)) // it means give me the time that happens after January, 1st, 1970 12:00 am by **zero** seconds
print("\(s2)") //1970-01-01 00:00:00

let s3 = NSDate() // it means the current time
print("\(s3)")//2015-10-25 14:12:40

let s4 = NSDate(timeIntervalSinceNow: NSTimeInterval(60)) //it means one minute (60 seconds) after the current time
print("\(s4)") //2015-10-25 14:13:40

let s5 = NSDate(timeIntervalSinceNow: NSTimeInterval(-60)) // it means one minute (60 seconds) before the current time
print("\(s5)") //2015-10-25 14:11:40

let sd = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60)) // it means one minute after the reference time (January, 1st, 1970: 12:00 am)
print("\(sd)") //2001-01-01 00:01:00

当然,如果你有一个 NSDate 对象,你可以简单地获取所有这些属性......

let sNow = NSDate()
sNow.timeIntervalSinceReferenceDate
sNow.timeIntervalSinceNow
sNow.timeIntervalSince1970

关于ios - timeIntervalSinceReferenceDate 与 timeIntervalSince1970 与 timeIntervalSinceNow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com