[英]Calculating the difference between two dates in Swift
我已经看到许多方法如何根据特定日期组件计算两个日期之间的差异,例如天、小时、月等(请参阅Stackoverflow 上的这个答案):
Calendar.current.dateComponents([.hour], from: fromDate, to: toDate).hour
Calendar.current.dateComponents([.day], from: fromDate, to: toDate).day
Calendar.current.dateComponents([.month], from: fromDate, to: toDate).month
我还没有看到如何使用实际的Date
对象进行计算。 就像是
func computeNewDate(from fromDate: Date, to toDate: Date) -> Date
let delta = toDate - fromDate
let today = Date()
if delta < 0 {
return today
} else {
return today + delta
}
}
我已经看到了 iOS 10 中引入的DateInterval
类型,但根据文档
[it] 不支持反向间隔,即持续时间小于 0 且结束日期早于开始日期的间隔。
这使得用日期计算本质上很困难——尤其是当您不知道哪个是较早的日期时。
是否有任何干净整洁的方法来直接计算Date
之间的时间差(并将它们再次添加到Date
实例)而不用它们的timeIntervalSinceReferenceDate
计算?
我最终为Date
创建了一个自定义运算符:
extension Date {
static func - (lhs: Date, rhs: Date) -> TimeInterval {
return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}
}
使用这个运算符,我现在可以在更抽象的层次上计算两个日期之间的差异,而无需关心timeIntervalSinceReferenceDate
或参考日期究竟是什么——并且不会丢失精度,例如:
let delta = toDate - fromDate
显然,我没有太大变化,但对我来说,它更具可读性和结果:Swift 已经为Date
和TimeInterval
实现了+
运算符:
/// Returns a `Date` with a specified amount of time added to it. public static func + (lhs: Date, rhs: TimeInterval) -> Date
所以它已经支持
Date + TimeInterval = Date
因此,它还应支持
Date - Date = TimeInterval
在我看来,这就是我通过-
运算符的简单实现添加的内容。 现在我可以简单地按照我的问题中提到的方式编写示例函数:
func computeNewDate(from fromDate: Date, to toDate: Date) -> Date
let delta = toDate - fromDate // `Date` - `Date` = `TimeInterval`
let today = Date()
if delta < 0 {
return today
} else {
return today + delta // `Date` + `TimeInterval` = `Date`
}
}
很可能这有一些我目前不知道的缺点,我很想听听您对此的看法。
您可以使用自定义运算符进行扩展,并返回元组
extension Date {
static func -(recent: Date, previous: Date) -> (month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?) {
let day = Calendar.current.dateComponents([.day], from: previous, to: recent).day
let month = Calendar.current.dateComponents([.month], from: previous, to: recent).month
let hour = Calendar.current.dateComponents([.hour], from: previous, to: recent).hour
let minute = Calendar.current.dateComponents([.minute], from: previous, to: recent).minute
let second = Calendar.current.dateComponents([.second], from: previous, to: recent).second
return (month: month, day: day, hour: hour, minute: minute, second: second)
}
}
使用:
let interval = Date() - updatedDate
print(interval.day)
print(interval.month)
print(interval.hour)
简单地toDate.timeIntervalSince(fromDate)
。
要在不添加任何扩展的情况下重新实现您的函数:
func computeNewDate(from fromDate: Date, to toDate: Date) -> Date {
let delta = toDate.timeIntervalSince(fromDate)
let today = Date()
if delta < 0 {
return today
} else {
return today.addingTimeInterval(delta)
}
}
我找到了一个内置解决方案来计算两个日期之间的差异。
let delta = toDate.timeIntervalSince(fromDate)
您可以使用 :
let delta = fromDate.distance(to: toDate)
比如说……
func computeNewDate(from fromDate: Date, to toDate: Date) -> Date {
let delta = Calendar.current.dateComponents([.second], from: fromDate, to: toDate).second!
return Calendar.current.date(byAdding: .second, value: delta, to: Date())!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.