Swift 3 Calendar.range - cant migrate funciton

HI, i have hard time migrating this function to swift 3, can someone help me


extension Date {

    func numberOfDaysUntilDateTime(toDateTime: NSDate, calendar:NSCalendar) -> Int {
       
        var fromDate: NSDate?, toDate: NSDate?
       
        calendar.rangeOfUnit(.Day, startDate: &fromDate, interval: nil, forDate: self)
        calendar.rangeOfUnit(.Day, startDate: &toDate, interval: nil, forDate: toDateTime)
       
        let difference = calendar.components(.Day, fromDate: fromDate!, toDate: toDate!, options: [])
       
        return difference.day
       
    }

}

Replies

I’m not exactly sure what your current code does but I suspect you want this:

extension Date {
    func numberOfDays(until then: Date, calendar: Calendar) -> Int { 
        return calendar.dateComponents([.day], from: self, to: then).day!
    } 
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"