0

I saw in the Apple's documentation about NSDate that Date objects represents an invariant time interval relative to an absolute reference date. How can I get the number of days since that Reference Date?

Edit: I saw solutions for a similar question, but I'm searching for a solution that don't force me to enter specific dates.

Cue
  • 2,952
  • 3
  • 33
  • 54
  • http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates ? – Larme May 08 '17 at 00:27
  • Possible duplicate of [Swift 3 - find number of calendar days between two dates](http://stackoverflow.com/questions/40075850/swift-3-find-number-of-calendar-days-between-two-dates) – Adrian May 08 '17 at 01:30

1 Answers1

2

You can use calendar method dateComponents from Date to Date and pass only the day component:

let date = Date(timeIntervalSinceReferenceDate: 0)     //  "2001-01-01 00:00:00 +0000" 
let days = Calendar.current.dateComponents([.day], from: date, to: Date()).day    // 5971
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thank you Leo! I was looking for something like that, that is, not to use any date. – Cue May 08 '17 at 08:31