Swift days between two NSDates

后端 未结 27 1578
清歌不尽
清歌不尽 2020-11-27 13:30

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:

27条回答
  •  醉酒成梦
    2020-11-27 13:42

    Nice handy one liner :

    extension Date {
      var daysFromNow: Int {
        return Calendar.current.dateComponents([.day], from: Date(), to: self).day!
      }
    }
    

提交回复
热议问题