Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the difference in two dates - Swift

Tags:

ios

swift

nsdate

I cannot get the date difference function to work. It says NSDate is not implicitly convertible to 'Date' but I do not see an immediate work around this; using as Date does not work.

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.
    }
}
like image 504
Zog Avatar asked Dec 06 '22 16:12

Zog


1 Answers

Change all of your NSDate to Date, then replace your requestedComponent with this:

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

Your difference will be:

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

FYI: Your dateFormatter doesn't work with this "25/12/16 00:00:00" here is your whole class in correct form:

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.
     }
}
like image 102
Siavash Alp Avatar answered Dec 31 '22 17:12

Siavash Alp