I'm wondering if there is some new and awesome possibility to get the amount of days between two NSDates in Swift / the "new" Cocoa?
E.g. like in Ruby I would do:
(end_date - start_date).to_i
I'm wondering if there is some new and awesome possibility to get the amount of days between two NSDates in Swift / the "new" Cocoa?
E.g. like in Ruby I would do:
(end_date - start_date).to_i
2017 version, copy and paste
You have to consider the time difference as well. For example if you compare the dates
2015-01-01 10:00
and2015-01-02 09:00
, days between those dates will return as 0 (zero) since the difference between those dates is less than 24 hours (it's 23 hours).If your purpose is to get the exact day number between two dates, you can work around this issue like this:
Swift 3 and Swift 4 Version
This returns an absolute difference in days between some
Date
and today:and then use it:
easier option would be to create a extension on Date
All answer is good. But for Localizations we need calculates a number of decimal days in between two dates. so we can provide the sustainable decimal format.
Erin's method updated to Swift 3, This shows days from today (disregarding time of day)