大跨度的日期 [英] Strideable Date
本文介绍了大跨度的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种以不同的增量跨越 Date
范围的好方法(秒数又称为 TimeInterval
或 DateComponents
又称为 .hour
, .minute
)
I'm looking for cool ways to stride through Date
ranges with different increments (either seconds aka TimeInterval
or with DateComponents
aka .hour
, .minute
)
import Foundation
extension Date: Strideable {
// typealias Stride = SignedInteger // doesn't work (probably because declared in extension
public func advanced(by n: Int) -> Date {
self.addingTimeInterval(TimeInterval(n))
}
public func distance(to other: Date) -> Int {
return Int(self.distance(to: other))
}
}
let now = Date()
let dayAfterNow = Date().addingTimeInterval(86400)
let dateRange = now ... dayAfterNow
let dateArr : [Date] = Array(stride(from: now, to: dayAfterNow, by: 60)) // Solves my task but not exactly how I wanted.
let formatter: DateFormatter = {
let df = DateFormatter()
df.timeStyle = .short
return df }()
print (dateArr.prefix(7).map { formatter.string(from: $0) })
/// This hoever doesn't work
// There must be a way to make it work but couldn't figure it out for now
let errDateArr: [Date] = Array(dateRange)
// Error: Initializer 'init(_:)' requires that 'Date.Stride' (aka 'Double') conform to 'SignedInteger'
问题的第二部分是,我也想拥有类似的东西:
The second part of the question is that also i'd like to have something like:
var components = DateComponents()
components.hour = 8
components.minute = 0
let date = Calendar.current.date(from: components)
let dateByComponentArr : [Date] = Array(stride(from: now, to: dayAfterNow, by: components))
推荐答案
正如@Alexander所提到的那样,您不应该尝试将Date设置为Strideable,但是您可以实现自己的方法来生成接下来的n天,几小时或几分钟:
As already mentionerd by @Alexander you shouldn't try to conform Date to Strideable but you can implement your own methods to generate the next n days, hours or minutes:
extension Date {
func year(using calendar: Calendar = .current) -> Int { calendar.component(.year, from: self) }
func month(using calendar: Calendar = .current) -> Int { calendar.component(.month, from: self) }
func day(using calendar: Calendar = .current) -> Int { calendar.component(.day, from: self) }
func hour(using calendar: Calendar = .current) -> Int { calendar.component(.hour, from: self) }
func minute(using calendar: Calendar = .current) -> Int { calendar.component(.minute, from: self) }
func nextDays(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
var days: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
days.append(DateComponents(calendar: calendar, year: year, month: month, day: day + x, hour: 12).date!)
}
return days
}
func nextHours(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
let hour = self.hour(using: calendar)
var hours: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
hours.append(DateComponents(calendar: calendar, year: year, month: month, day: day, hour: hour + x).date!)
}
return hours
}
func nextMinutes(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
let hour = self.hour(using: calendar)
let minute = self.minute(using: calendar)
var minutes: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
minutes.append(DateComponents(calendar: calendar, year: year, month: month, day: day, hour: hour, minute: minute + x).date!)
}
return minutes
}
}
extension Date {
static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter
}()
}
Playgournd测试:
Playgournd testing:
let days = Date().nextDays(n: 10)
for day in days {
print(Date.formatter.string(from: day))
}
let hours = Date().nextHours(n: 10)
for hour in hours {
print(Date.formatter.string(from: hour))
}
let minutes = Date().nextMinutes(n: 10)
for minute in minutes {
print(Date.formatter.string(from: minute))
}
这篇关于大跨度的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文