时间戳 NSTimeInterval

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

//获取当前时间
let now = NSDate()

// 创建一个日期格式器
let fmt = NSDateFormatter()
fmt.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
print("当前日期时间:\(fmt.stringFromDate(now))")

//当前时间的时间戳
let timeInterval:NSTimeInterval = now.timeIntervalSince1970
let timeIntervalInt = Int(timeInterval)
print("当前时间的时间戳:\(timeIntervalInt) ++ \(timeInterval)")

//将时间戳转为日期时间
let date = NSDate(timeIntervalSince1970: timeInterval)
print("对应的日期时间:\(fmt.stringFromDate(date))")


// 当前时间, 60*60(1小时)后的时间
let otherDate = NSDate(timeInterval: 60*60, sinceDate: now)
print("1小时后 日期时间:\(fmt.stringFromDate(otherDate))")

// 时间戳
// 晚 timeIntervalSinceDate 早 === 晚 - 早 === 正数 === 间隔秒钟
let difference = otherDate.timeIntervalSinceDate(now)
print("间隔秒钟:\(difference)")

时间戳转分钟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 天 时 分 秒
var timeInterval = (60*60*24*2) + (60*60*5) + (60*3) + (20)
// 时间戳 --> 分钟
var dateStr = ""
if timeInterval >= 60*60*24 {
let v = timeInterval / (60*60*24)
timeInterval -= (60*60*24) * v
dateStr += "\(v)天"
}
if timeInterval >= 60*60 {
let v = timeInterval / (60*60)
timeInterval -= (60*60) * v
dateStr += "\(v)小时"
}
if timeInterval >= 60 {
let v = timeInterval / (60)
timeInterval -= (60) * v
dateStr += "\(v)分"
}
dateStr += "\(timeInterval)秒"
print("\(dateStr)") // 2天5小时3分20秒

本文标题:时间戳 NSTimeInterval

文章作者:史彦超

发布时间:2016年10月11日 - 22:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2016/10/11/2016-10-11-%E6%97%B6%E9%97%B4%E6%88%B3NSTimeInterval/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here