Swift days between two NSDates

后端 未结 27 1446
清歌不尽
清歌不尽 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条回答
  •  -上瘾入骨i
    2020-11-27 13:33

    Here is my answer for Swift 2:

    func daysBetweenDates(startDate: NSDate, endDate: NSDate) -> Int
    {
        let calendar = NSCalendar.currentCalendar()
    
        let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
    
        return components.day
    }
    

提交回复
热议问题