标签的持续时间
Time Duration From Labels
我查看了此处与获取持续时间有关的所有代码。我需要做的就是简单地以这种格式 hr:m:s 像 20:23:04 这样的标签中的时间为例。我需要从 currentTime.StringValue 和 oldTime.StringValue 中获取时间,然后让它显示 durationOfTime.StringValue 中的持续时间。
例如说 currentTime = 11:00:04 和 oldTime = 9:00:04 那么我想要 durationOfTime = 2hr's.
现在就是这样,我有一个代码可以获取时间并以上述格式存储它。然而,我 运行 遇到了各种各样的问题,在这里尝试不同的代码,这些代码与持续时间有关。
使用 2 个格式化程序:DateFormatter
将字符串转换为 Date
对象,DateComponentFormatter
显示 2 个 Date
之间的持续时间:
let currentTimeStr = "11:00:04"
let oldTimeStr = "9:00:04"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "H:mm:ss"
let dateComponentFormatter = DateComponentsFormatter()
dateComponentFormatter.allowedUnits = [.hour, .minute]
dateComponentFormatter.unitsStyle = .short
if let currentTime = dateFormatter.date(from: currentTimeStr),
let oldTime = dateFormatter.date(from: oldTimeStr),
let durationStr = dateComponentFormatter.string(from: oldTime, to: currentTime)
{
print(durationStr) // 2 hrs
}
希望这对您有所帮助。编辑了 maddy 的建议。
let currentTime = "11:00:04"
let oldTime = "9:00:04"
let currentTimeComponents = currentTime.components(separatedBy: ":")
let oldTimeComponents = oldTime.components(separatedBy: ":")
let hrsDifference = Int(currentTimeComponents[0])! - Int(oldTimeComponents[0])!
let minutesDifference = Int(currentTimeComponents[1])! - Int(oldTimeComponents[1])!
let secondsDifference = Int(currentTimeComponents[2])! - Int(oldTimeComponents[2])!
print("hrsDifference = ",hrsDifference)
print("minutesDifference = ",minutesDifference)
print("secondsDifference = ",secondsDifference)
我查看了此处与获取持续时间有关的所有代码。我需要做的就是简单地以这种格式 hr:m:s 像 20:23:04 这样的标签中的时间为例。我需要从 currentTime.StringValue 和 oldTime.StringValue 中获取时间,然后让它显示 durationOfTime.StringValue 中的持续时间。
例如说 currentTime = 11:00:04 和 oldTime = 9:00:04 那么我想要 durationOfTime = 2hr's.
现在就是这样,我有一个代码可以获取时间并以上述格式存储它。然而,我 运行 遇到了各种各样的问题,在这里尝试不同的代码,这些代码与持续时间有关。
使用 2 个格式化程序:DateFormatter
将字符串转换为 Date
对象,DateComponentFormatter
显示 2 个 Date
之间的持续时间:
let currentTimeStr = "11:00:04"
let oldTimeStr = "9:00:04"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "H:mm:ss"
let dateComponentFormatter = DateComponentsFormatter()
dateComponentFormatter.allowedUnits = [.hour, .minute]
dateComponentFormatter.unitsStyle = .short
if let currentTime = dateFormatter.date(from: currentTimeStr),
let oldTime = dateFormatter.date(from: oldTimeStr),
let durationStr = dateComponentFormatter.string(from: oldTime, to: currentTime)
{
print(durationStr) // 2 hrs
}
希望这对您有所帮助。编辑了 maddy 的建议。
let currentTime = "11:00:04"
let oldTime = "9:00:04"
let currentTimeComponents = currentTime.components(separatedBy: ":")
let oldTimeComponents = oldTime.components(separatedBy: ":")
let hrsDifference = Int(currentTimeComponents[0])! - Int(oldTimeComponents[0])!
let minutesDifference = Int(currentTimeComponents[1])! - Int(oldTimeComponents[1])!
let secondsDifference = Int(currentTimeComponents[2])! - Int(oldTimeComponents[2])!
print("hrsDifference = ",hrsDifference)
print("minutesDifference = ",minutesDifference)
print("secondsDifference = ",secondsDifference)