将Unix(Epoch)时间转换为本地时间

时间:2016-02-20 07:23:00

标签: ios swift

API我使用返回时间作为Unix时间(1424952512)到目前为止,我可以使用

将unix时间转换为NSDate
func 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 
}

现在我如何将datenowDate进行比较并打印小时或分钟的差异?

1 个答案:

答案 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")
}