调用时直接传入秒数即可;需要注意的参数属性为 : .pad 和 .positional;根据自己的需求变动;
、、、
func FormatDate(second:Int) ->String{
let formatter = DateComponentsFormatter.init()
//.pad为0:00:00格式 其他的我测试为 当时分秒任意为0时省略
formatter.zeroFormattingBehavior = .pad
formatter.allowedUnits = NSCalendar.Unit(rawValue: NSCalendar.Unit.day.rawValue | NSCalendar.Unit.hour.rawValue | NSCalendar.Unit.minute.rawValue | NSCalendar.Unit.second.rawValue)
//.positional 是数字:数字:数字格式 其他选项我测试为中文格式
formatter.unitsStyle=DateComponentsFormatter.UnitsStyle.positional
returnformatter.string(from:TimeInterval(second)) ??""
}
、、、
网友评论