计算属于 2 个日期的持续时间

Calculate Time Duration that fall within 2 dates

如何计算介于 2 个日期集之间的时间(持续时间)? (小时和分钟 - 所有时间均为 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")!

//标记:例1

从低于上述日期集的开始日期和结束日期算出持续时间。

let start1 = dateFormatter.date(from: "2020-10-06 17:10:00")!
let end1 = dateFormatter.date(from: "2020-10-07 03:50:00")!

Should return: 02:10 (2h 10min)

//标记:例2

let start2 = dateFormatter.date(from: "2020-10-06 17:10:00")!
let end2 = dateFormatter.date(from: "2020-10-07 07:59:45")!

Should return: 03:10 (3h 10min)

您要找的是 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"

你的 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())