是否可以格式化 dateComponent 以显示为 hh:mm:ss?

Is it possible to format dateComponent to display as hh:mm:ss?

我正在尝试以 hh:mm:ss 格式显示倒计时,但由于我输出的不是日期而是 dateComponents,因此我无法使用 formatter.dateFormat。是否可以格式化 dateComponents?

目前我得到如下所示的输出:

小时:5,分钟:12,秒:45

func nextEventCalculator() -> DateComponents {
    let now = Date()
    let calendar = Calendar.current
    let components = DateComponents(calendar: calendar, hour: 12, minute: 15)  // <- 12:15 pm
    let nextEvent = calendar.nextDate(after: now, matching: components, matchingPolicy: .nextTime)!

    let diff = calendar.dateComponents([.hour, .minute, .second], from: now, to: nextEvent)
    dateCountdownLabel.text = "\(diff)"
    return diff
}

使用 DateComponentsFormatter with a unitsStyle.positional:

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
dateCountdownLabel.text = formatter.string(from: diff)