计算 2 个日期内的持续时间
Posted
技术标签:
【中文标题】计算 2 个日期内的持续时间【英文标题】:Calculate Time Duration that fall within 2 dates 【发布时间】:2020-10-28 07:42:25 【问题描述】:如何计算两个日期集之间的时间(持续时间)? (小时和分钟 - UTC 的所有时间)
// 2 date sets (2 daylight periods)
let dateFormatter = DateFormatter()
let dayStart1 = dateFormatter.date(from: "2020-10-06 07:02:42")!
let dayEnd1 = dateFormatter.date(from: "2020-10-06 19:20:46")!
let dayStart2 = dateFormatter.date(from: "2020-10-07 06:59:40")!
let dayEnd2 = dateFormatter.date(from: "2020-10-07 18:58:12")!
//MARK:示例1
根据上述日期集中的开始和结束日期计算持续时间。
let start1 = dateFormatter.date(from: "2020-10-06 17:10:00")!
let end1 = dateFormatter.date(from: "2020-10-07 03:50:00")!
应该返回:02:10(2 小时 10 分钟)
//MARK:示例2
let start2 = dateFormatter.date(from: "2020-10-06 17:10:00")!
let end2 = dateFormatter.date(from: "2020-10-07 07:59:45")!
应该返回:03:10(3 小时 10 分钟)
【问题讨论】:
不明白你的问题。您是如何得出 start1/end1 start2/end2 的预期结果的?他们没有意义 【参考方案1】:您要查找的内容称为 DateInterval。您可以简单地根据您的日期创建日期间隔并获取其交点。然后你可以总结结果的持续时间:
let fmt = DateFormatter()
fmt.locale = .init(identifier: "en_US_POSIX")
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
fmt.timeZone = TimeZone(secondsFromGMT: 0)!
let dayStart1 = fmt.date(from: "2020-10-06 07:02:42")!
let dayEnd1 = fmt.date(from: "2020-10-06 19:20:46")!
let dayStart2 = fmt.date(from: "2020-10-07 06:59:40")!
let dayEnd2 = fmt.date(from: "2020-10-07 18:58:12")!
let di1 = DateInterval(start: dayStart1, end: dayEnd1)
let di2 = DateInterval(start: dayStart2, end: dayEnd2)
let start1 = fmt.date(from: "2020-10-06 17:10:00")!
let end1 = fmt.date(from: "2020-10-07 03:50:00")!
let workout = DateInterval(start: start1, end: end1)
let intersection1 = di1.intersection(with: workout)
let intersection2 = di2.intersection(with: workout)
let duration = (intersection1?.duration ?? .zero) + (intersection2?.duration ?? .zero)
let dcf = DateComponentsFormatter()
dcf.string(from: duration) // "2:10:46"
let start2 = fmt.date(from: "2020-10-06 17:10:00")!
let end2 = fmt.date(from: "2020-10-07 07:59:45")!
let workout2 = DateInterval(start: start2, end: end2)
let intersection3 = di1.intersection(with: workout2)
let intersection4 = di2.intersection(with: workout2)
let duration2 = (intersection3?.duration ?? .zero) + (intersection4?.duration ?? .zero)
dcf.string(from: duration2) // "3:10:51"
【讨论】:
【参考方案2】:您使用 example1 和 example2 的结果是错误的 将这些扩展添加到您的项目中
extension TimeInterval
func stringFromTimeInterval() -> String
let time = NSInteger(self)
let seconds = time % 60
let minutes = (time / 60) % 60
let hours = (time / 3600)
return String(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds)
extension Date
static func getTimeInterval(to: Date, from: Date) -> TimeInterval
return to.timeIntervalSinceReferenceDate - from.timeIntervalSinceReferenceDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let start1 = dateFormatter.date(from: "2020-10-06 17:10:00")!
let end1 = dateFormatter.date(from: "2020-10-07 07:59:45")!
print(Date.getTimeInterval(to: end1, from: start1).stringFromTimeInterval())
【讨论】:
以上是关于计算 2 个日期内的持续时间的主要内容,如果未能解决你的问题,请参考以下文章