我想像这样创建一个时间元组列表;每小时从 0 点到 23 点。
tuple = [(2017-03-24 00:00:00 +0000, 0.0), (2017-03-24 00:01:00 +0000, 0.0), (2017-03-24 00:02:00 +0000, 0.0), (2017-03-24 00:03:00 +0000, 0.0)]
我试图使用这段代码来给我小时和分钟,但是我似乎无法弄清楚如何添加当前的月份、日期和年份。或者在 Struct 中调整日期格式。
谢谢您的帮助。这是我正在使用的结构......
import Foundation
struct TimeDay {
let start: TimeInterval
let end: TimeInterval
let interval: TimeInterval
init(start: TimeInterval, interval: TimeInterval, end: TimeInterval) {
self.start = start
self.interval = interval
self.end = end
}
init(startHour: TimeInterval, intervalMinutes: TimeInterval, endHour: TimeInterval) {
self.start = startHour * 60 * 60
self.end = endHour * 60 * 60
self.interval = intervalMinutes * 60
}
var timeRepresentations: [String] {
let dateComponentFormatter = DateComponentsFormatter()
dateComponentFormatter.unitsStyle = .positional
dateComponentFormatter.allowedUnits = [.minute, .hour]
let dateComponent = NSDateComponents()
return timeIntervals.map { timeInterval in
dateComponent.second = Int(timeInterval)
return dateComponentFormatter.string(from: dateComponent as DateComponents)!
}
}
var timeIntervals: [TimeInterval]{
return Array(stride(from: start, through: end, by: interval))
}
}