用在NSTextField中获得的值减去小时

时间:2018-08-20 13:05:46

标签: ios swift macos

下面的代码在一个NSTextField中使用当前时间,在另一个NSTextField中使用参考时间

let timeZone = TimeZone(identifier: "Europe/Amsterdam")        
let dateFormatter = NSDate()
let hora = dateFormatter.date(withCalendarFormat: lblLastClick.stringValue, timeZone: timeZone)
let horaAtual = dateFormatter.date(withCalendarFormat: lblClock.stringValue, timeZone: timeZone)

我需要从当前时间中减去参考时间,看看已经过去了多少分钟,我无法使它正常工作,我已经尝试了很多方法。

1 个答案:

答案 0 :(得分:0)

let difference = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date(), to: date)

        let strYear = String(difference.year!)
        let strMonth = "-" + String(difference.month!)
        let strDays = "-" + String(difference.day!)
        let strHours = String(difference.hour!)
        let strMinit = ":" + String(difference.minute!)
        let strSec = ":" + String(difference.second!)