美文网首页
倒计时毫秒 Calendar处理 非时间戳方法

倒计时毫秒 Calendar处理 非时间戳方法

作者: 开始就会被黏住 | 来源:发表于2021-09-06 20:49 被阅读0次

纠结处: 系统未提供毫秒 😸 

核心: 纳秒可以转毫秒

备注:  我这里是百毫秒一变 timer就是0.1秒 具体根据你的情况处理

private func compareCurrentTimeOfSize() ->String {

        /// expireData 是后台返回时间转成的Date

        guard let endDate = expireData else return ""}

        let nowDate = Date()

        let calendar: Calendar = Calendar.current

        let commponent: DateComponents = calendar.dateComponents([.hour, .minute, .second, .nanosecond], from: nowDate, to: endDate)

        guard commponent.hour! >0 || commponent.minute! >0 || commponent.second! > 0 else 

            return "拼团已结束".localized

        }

        //- 时

        var hStr: String!

        if commponent.hour! <10 {

            hStr = String(format:"0\(commponent.hour!)")

        } else {

            hStr = String(format:"\(commponent.hour!)")

        }

        //- 分

        var mStr:String!

        if commponent.minute! <10{

            mStr = String(format:"0\(commponent.minute!)")

        } else {

            mStr = String(format:"\(commponent.minute!)")

        }

        //- 秒

        var sStr: String!

        if commponent.second! <10 {

            sStr = String(format:"0\(commponent.second!)")

        } else {

            sStr = String(format:"\(commponent.second!)")

        }

        //- 毫秒

        var ssStr: String!

        if commponent.nanosecond! <1000000{

            ssStr ="0"

        } else {

            /// 除1000000 是毫秒 除100是转百毫秒 为单位 根据需要处理

            ssStr = String(format:"\(commponent.nanosecond!/1000000/100)")

        }

         returnString.init(format:"%@%@:%@:%@.%@","剩余:".localized, hStr, mStr, sStr, ssStr)

    }

相关文章

网友评论

      本文标题:倒计时毫秒 Calendar处理 非时间戳方法

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