我想创建一个像这样的元组列表;每小时从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)]
我曾尝试使用这段代码,这会给我时间和分钟,但我似乎无法弄清楚如何添加当前月,日和年。或者在结构中调整日期格式。
感谢您的帮助。这是我正在使用的结构......
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))
}
}
答案 0 :(得分:1)
一个月可能有28到31天。您无法将其转换为精确的秒数。使用DateComponents
存储您的步骤:
struct TimeDay {
var startDate: Date
var endDate: Date
var step: DateComponents
var calendar = Calendar.autoupdatingCurrent
var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()
init(startDate: Date, endDate: Date, step: DateComponents) {
self.startDate = startDate
self.endDate = endDate
self.step = step
}
var timeIntervals : [Date] {
guard self.startDate <= self.endDate else {
return []
}
var result = [self.startDate]
var date = self.startDate
while date < self.endDate {
date = self.calendar.date(byAdding: step, to: date)!
result.append(date)
}
return result
}
var timeRepresentation : [String] {
return self.timeIntervals.map { self.dateFormatter.string(from: $0) }
}
}
示例1:按小时迭代
let startDate1 = DateComponents(calendar: .current, year: 2017, month: 3, day: 24).date!
let endDate1 = DateComponents(calendar: .current, year: 2017, month: 3, day: 25).date!
let t1 = TimeDay(startDate: startDate1, endDate: endDate1, step: DateComponents(hour: 1))
print(t1.timeRepresentation)
示例2:按月迭代
// Iterate by month
let startDate2 = DateComponents(calendar: .current, year: 2017, month: 1, day: 1).date!
let endDate2 = DateComponents(calendar: .current, year: 2017, month: 12, day: 31).date!
let t2 = TimeDay(startDate: startDate2, endDate: endDate2, step: DateComponents(month: 1))
print(t2.timeRepresentation)
示例3:您还可以将时间单位混淆并迭代1个月和5天:
let t3 = TimeDay(startDate: startDate2, endDate: endDate2, step: DateComponents(month: 1, day: 5))
print(t3.timeRepresentation)
修改强>
如果你想找到最近的小时:(1)前进到下一个小时; (2)向后移动到最后一小时; (3)选择最接近指定日期的那个
let date = Date()
let closestHour = ([.backward, .forward] as [Calendar.SearchDirection])
.flatMap { Calendar.current.nextDate(after: date, matching: DateComponents(minute: 0), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: $0) }
.min { abs($0.timeIntervalSince(date)) < abs($1.timeIntervalSince(date)) }
答案 1 :(得分:0)
你的意思是这样的吗?
struct TimeDay {
let start: TimeInterval
let end: TimeInterval
let interval:TimeInterval
let fmt = DateFormatter()
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] {
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss z"
var dateComponent = DateComponents()
guard let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian) else {
return []
}
dateComponent = calendar.components(in: TimeZone.current, from:Date())
dateComponent.hour = 0
dateComponent.minute = 0
return timeIntervals.map { timeInterval in
dateComponent.second = Int(timeInterval)
let dt = calendar.date(from:dateComponent)
return fmt.string(from:dt!)
}
}
var timeIntervals: [TimeInterval]{
return Array(stride(from: start, through: end, by: interval))
}
}
您可能需要调整dateFormat
的{{1}}以获得您想要的确切输出,但这应该以您想要的方式工作。