我正在尝试在Swift 2中学习NSDate
类。
我发现每个NSDate对象都有三个属性,分别是:
我有点困惑。尽管我知道它们代表秒,但我无法理解它们的解释。
我做了这段代码:
let whatISThis = NSDate(timeIntervalSinceReferenceDate: NSTimeInterval(60))
好吧,我有个约会,那是什么?这三个属性有什么区别?
最佳答案
timeIntervalSinceReferenceDate
是自2001年1月1日以来的秒数:凌晨12:00timeIntervalSince1970
是自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