调用时直接传入秒数即可;需要注意的参数属性为 : .pad 和 .positional;根据自己的需求变动;
、、、
// second 秒数
func FormatDate(second:Int) ->String{
let formatter = DateComponentsFormatter.init()
// . dropMiddle为 0d 00h 00m 格式 (需要其它格式可以自己点进去看看)
formatter.zeroFormattingBehavior = .dropMiddle
// 此处事例只写了 日 时 分;需要秒的可以在后面加上(参数: | NSCalendar.Unit.second.rawValue )
formatter.allowedUnits = NSCalendar.Unit(rawValue: NSCalendar.Unit.day.rawValue | NSCalendar.Unit.hour.rawValue | NSCalendar.Unit.minute.rawValue)
formatter.unitsStyle = DateComponentsFormatter.UnitsStyle.abbreviated
// 结果默认格式为 1d 1h 1m
var resultStr = formatter.string(from: TimeInterval(seconds)) ?? ""
// 处理为 1天 1小时 1分钟 (根据自己需求处理)
resultStr = resultStr.replacingOccurrences(of: "d", with: "天", options: .literal, range: nil)
resultStr = resultStr.replacingOccurrences(of: "h", with: "小时", options: .literal, range: nil)
resultStr = resultStr.replacingOccurrences(of: "m", with: "分钟", options: .literal, range: nil)
return resultStr
}
、、、