DateComponentsFormatter不允许小数单位

时间:2017-12-20 14:34:05

标签: swift nsdateformatter nsdatecomponents

我无法让DateComponentsFormatter打印出小数单位!

以下是 Swift 4 中的代码:

let duration = 5400   // 1.5 hours in seconds

let dateComponentsFormatter = DateComponentsFormatter.init()
dateComponentsFormatter.allowedUnits = [.hour]
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.allowsFractionalUnits = true
dateComponentsFormatter.unitsStyle = .short

let stringDuration = dateComponentsFormatter.string(from: number)
print(stringDuration!)

我玩allowedUnitsmaximumUnitCount,但我只能打印“1小时”“2小时”

如何制作“1.5小时”

0 个答案:

没有答案