Swift days between two NSDates

后端 未结 27 1576
清歌不尽
清歌不尽 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:53

    I see a couple Swift3 answers so I'll add my own:

    public static func daysBetween(start: Date, end: Date) -> Int {
       Calendar.current.dateComponents([.day], from: start, to: end).day!
    }
    

    The naming feels more Swifty, it's one line, and using the latest dateComponents() method.

提交回复
热议问题