2017-05-08 66 views
0

我在Apple's documentation中看到关于NSDate Date对象表示相对于绝对引用日期的不变时间间隔。我怎样才能得到自参考日期以来的天数?如何获取自NSDate参考日期以来的天数?

编辑:我看到类似问题的解决方案,但我正在寻找一种解决方案,不会强制我输入特定的日期。

+0

http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates ? – Larme

+0

[Swift 3 - 找到两个日期之间的日历天数]的可能重复(http://stackoverflow.com/questions/40075850/swift-3-find-number-of-calendar-days-between-two-dates) – Adrian

回答

1

您可以使用日历法dateComponents从日期到日期,并通过仅一天组件:

let date = Date(timeIntervalSinceReferenceDate: 0)  // "2001-01-01 00:00:00 +0000" 
let days = Calendar.current.dateComponents([.day], from: date, to: Date()).day // 5971 
+0

谢谢Leo!我正在寻找那样的东西,那就是不要使用任何日期。 – Cue

相关问题