使用Swift 3,您可以根据需要选择以下两种方法之一来解决问题。
1.向用户显示两个日期之间的时差
您可以使用DateComponentsFormatter
为应用程序界面创建字符串。DateComponentsFormatter
具有maximumUnitCount
带有以下声明的属性:
var maximumUnitCount: Int { get set }
使用此属性可以限制在结果字符串中显示的单位数。例如,将此属性设置为2,而不是“ 1h 10m,30s”,结果字符串将是“ 1h 10m”。当您受空间限制或想要将值四舍五入到最接近的大单位时,请使用此属性。
通过将maximumUnitCount
的值设置为1
,可以确保仅以一个DateComponentsFormatter
单位(年,月,日,小时或分钟)显示差异。
下面的Playground代码显示了如何显示两个日期之间的时差:
import Foundation
let oldDate = Date(timeIntervalSinceReferenceDate: -16200)
let newDate = Date(timeIntervalSinceReferenceDate: 0)
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [NSCalendar.Unit.year, .month, .day, .hour, .minute]
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.unitsStyle = DateComponentsFormatter.UnitsStyle.full
let timeDifference = dateComponentsFormatter.string(from: oldDate, to: newDate)
print(String(reflecting: timeDifference)) // prints Optional("5 hours")
请注意,DateComponentsFormatter
此结果取整。因此,相差4小时30分钟将显示为5小时。
如果需要重复此操作,则可以重构代码:
import Foundation
struct Formatters {
static let dateComponentsFormatter: DateComponentsFormatter = {
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [NSCalendar.Unit.year, .month, .day, .hour, .minute]
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.unitsStyle = DateComponentsFormatter.UnitsStyle.full
return dateComponentsFormatter
}()
}
extension Date {
func offset(from: Date) -> String? {
return Formatters.dateComponentsFormatter.string(from: oldDate, to: self)
}
}
let oldDate = Date(timeIntervalSinceReferenceDate: -16200)
let newDate = Date(timeIntervalSinceReferenceDate: 0)
let timeDifference = newDate.offset(from: oldDate)
print(String(reflecting: timeDifference)) // prints Optional("5 hours")
2.获取两个日期之间的差异而无需格式化
如果您不需要使用格式来显示两个日期之间的差异,可以使用Calendar
。Calendar
有一个dateComponents(_:from:to:)
具有以下声明的方法:
func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
返回两个日期之间的差。
下面使用的Playground代码dateComponents(_:from:to:)
显示了如何通过仅返回一种类型Calendar.Component
(年,月,日,小时或分钟)中的差异来检索两个日期之间的差异。
import Foundation
let oldDate = Date(timeIntervalSinceReferenceDate: -16200)
let newDate = Date(timeIntervalSinceReferenceDate: 0)
let descendingOrderedComponents = [Calendar.Component.year, .month, .day, .hour, .minute]
let dateComponents = Calendar.current.dateComponents(Set(descendingOrderedComponents), from: oldDate, to: newDate)
let arrayOfTuples = descendingOrderedComponents.map { ($0, dateComponents.value(for: $0)) }
for (component, value) in arrayOfTuples {
if let value = value, value > 0 {
print(component, value) // prints hour 4
break
}
}
如果需要重复此操作,则可以重构代码:
import Foundation
extension Date {
func offset(from: Date) -> (Calendar.Component, Int)? {
let descendingOrderedComponents = [Calendar.Component.year, .month, .day, .hour, .minute]
let dateComponents = Calendar.current.dateComponents(Set(descendingOrderedComponents), from: from, to: self)
let arrayOfTuples = descendingOrderedComponents.map { ($0, dateComponents.value(for: $0)) }
for (component, value) in arrayOfTuples {
if let value = value, value > 0 {
return (component, value)
}
}
return nil
}
}
let oldDate = Date(timeIntervalSinceReferenceDate: -16200)
let newDate = Date(timeIntervalSinceReferenceDate: 0)
if let (component, value) = newDate.offset(from: oldDate) {
print(component, value) // prints hour 4
}
NSDateComponentsFormatter
。这是非常可配置的,可让您获得适当的简洁结果(例如.maximumUnitCount = 1
)。