📚 开源资源分享——颜佐光的个人书架
✉️ 联系站长
书架 / Mac / swift

Swift 时间

2021-05-26 15:57 · 约 624 字 · 浏览 2,059 次 · 更新 2021-05-26 15:58swift

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")
    }
}
← 返回《Mac》目录书架 →