1 Reply
      Latest reply: Sep 16, 2016 3:07 AM by eskimo RSS
      Petar Nes Level 1 Level 1 (0 points)

        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
               
            }
        
        }
        
        • Re: Swift 3 Calendar.range - cant migrate funciton
          eskimo Apple Staff Apple Staff (7,190 points)

          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"