如何获取当前日期unix-epoch
?timeIntervalSince1970
打印当前时间。有什么办法可以获取今天凌晨12点的时间?
例如,当前时间是:2018年1月7日下午5:30。timeIntervalSince1970
将打印当前时间,即1546903800000
。纪元系统中的当前日期为2018年1月7日00:00 AM。即1546848000000
我会用组件来做到这一点。
假设您需要按秒定义的时间time(2)
。如果您需要以定义的毫秒数time(3)
,则可以将其乘以1000。
// Get right now as it's `DateComponents`.
let now = Calendar.current.dateComponents(in: .current, from: Date())
// Create the start of the day in `DateComponents` by leaving off the time.
let today = DateComponents(year: now.year, month: now.month, day: now.day)
let dateToday = Calendar.current.date(from: today)!
print(dateToday.timeIntervalSince1970)
// Add 1 to the day to get tomorrow.
// Don't worry about month and year wraps, the API handles that.
let tomorrow = DateComponents(year: now.year, month: now.month, day: now.day! + 1)
let dateTomorrow = Calendar.current.date(from: tomorrow)!
print(dateTomorrow.timeIntervalSince1970)
您可以减去1得到昨天。
如果您需要在世界通用时间(UTC,GMT,Z…您给世界通用名称指定的任何名称),请使用以下命令。
let utc = TimeZone(abbreviation: "UTC")!
let now = Calendar.current.dateComponents(in: utc, from: Date())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句