API我使用返回时间作为Unix时间(1424952512)到目前为止,我可以使用
将unix时间转换为NSDatefunc timeStamp(unixTime: Double)-> NSDate {
let interval:NSTimeInterval = unixTime
let date:NSDate = NSDate(timeIntervalSince1970: interval)
print(date)
//now date
let nowDate = NSDate()
print(nowDate)
return date
}
现在我如何将date
与nowDate
进行比较并打印小时或分钟的差异?
答案 0 :(得分:0)
例如NSDateComponentsFormatter
let formatter = NSDateComponentsFormatter()
formatter.allowedUnits = [.Hour, .Minute]
formatter.unitsStyle = .Short
if let difference = formatter.stringFromDate(date, toDate: nowDate) {
print(difference)
} else {
print("invalid difference")
}