Obtener la diferencia en dos dates: Swift

No puedo hacer que funcione la function de diferencia de date. Dice que NSDate no es implícitamente convertible a 'Fecha', pero no veo un trabajo inmediato en torno a esto; usando as Date no funciona.

 import UIKit class ViewController: UIViewController { @IBOutlet weak var dateLabelOutlet: UILabel! let currentDate = NSDate() let dateFormatter = DateFormatter() let userCalendar = NSCalendar.current let requestedComponent: NSCalendar.Unit = [ NSCalendar.Unit.month, NSCalendar.Unit.day, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second, ] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } func printTime(){ dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss a" let startTime = NSDate() let endTime = dateFormatter.date(from: "25/12/16 00:00:00") let timeDifference = userCalendar.components(requestedComponent, from: startTime, to: endTime!, options: []) dateLabelOutlet.text = "\(timeDifference.month) Months \(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Cambie todo su NSDate a Date , luego reemplace su NSDate requestedComponent con esto:

 let requestedComponent: Set<Calendar.Component> = [ .month, .day, .hour, .minute, .second] 

Tu diferencia será:

 let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!) 

FYI: Your dateFormatter no funciona con este "25/12/16 00:00:00" aquí está toda su class en la forma correcta:

 class ViewController: UIViewController { @IBOutlet weak var dateLabelOutlet: UILabel! let currentDate = Date() let dateFormatter = DateFormatter() let userCalendar = Calendar.current let requestedComponent: Set<Calendar.Component> = [.month,.day,.hour,.minute,.second] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } func printTime() { dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss" let startTime = Date() let endTime = dateFormatter.date(from: "25/12/16 00:00:00") let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!) dateLabelOutlet.text = "\(timeDifference.month) Months \(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }