美文网首页
swift 毫秒或秒 转 时间 (天日时分)

swift 毫秒或秒 转 时间 (天日时分)

作者: Zero_Jin | 来源:发表于2020-08-26 16:28 被阅读0次

    调用时直接传入秒数即可;需要注意的参数属性为 : .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)) ??""
    
    }
    

    、、、

    相关文章

      网友评论

          本文标题:swift 毫秒或秒 转 时间 (天日时分)

          本文链接:https://www.haomeiwen.com/subject/lehfsktx.html