I am updating some of my old Swift 2 answers to Swift 3. My answer to this question, though, is not easy to update since the question specifically asks for NSDate
and not Date
. So I am creating a new version of that question that I can update my answer for.
Question
If I start with a Date
instance like this
let someDate = Date()
how would I convert that to an integer?
Related but different
These questions are asking different things:
Date
toInt
Doing the
Double
toInt
conversion causes the milliseconds to be lost. If you need the milliseconds then multiply by 1000 before converting toInt
.Int
toDate
Including the reverse for completeness.
I could have also used
timeIntervalSinceReferenceDate
instead oftimeIntervalSince1970
as long as I was consistent. This is assuming that the time interval is in seconds. Note that Java uses milliseconds.Note
NSDate
, see this answer.