Swift 时间

/ Mac / 没有评论 / 1381浏览

Swift 时间

timeInterval 指的是两个时间间隔的秒数.比如

let now = Date().timeIntervalSince1970 // 从1970距今的秒数

这个返回的即时unix时间戳

自定义时间

// 一天是86400秒 
let yestoday = Date(timeIntervalSinceNow:-86400).timeIntervalSince1970 

时间比较

func compareDate(_ dateA: Date, withAnotherDate dateB: Date) {
    let result = dateA.compare(dateB)
    if  result == ComparisonResult.orderedAscending {
        print("now compared with feture")
    } else if result == ComparisonResult.orderedDescending {
        print("now compared with past")
    } else {
        print("I think you compared with yourself")
    }
}