在10 [重复]下添加0到小时/分钟(add 0 to hours/minutes under 10 [duplicate])
这个问题在这里已有答案:
Swift 8答案中 Int的前导零我有一些问题要在swift3中找到一个合适的解决方案来做到这一点:
我有一个像10h20m的日期。 当我的小时数低于10时,它将是“5h30”。 我想出去“05h30”。
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = String(Int(difference!) / 3600) let minutes = String(Int(difference!) / 60 % 60) let time = "\(hours)h\(minutes)m" return time }如果有人知道怎么做,那么简单地感谢你!
This question already has an answer here:
Leading zeros for Int in Swift 9 answersi've some issue to found a proper solution in swift3 to do this :
I have a date like 10h20m. When my hours are under 10 it will be "5h30". I want to out "05h30".
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = String(Int(difference!) / 3600) let minutes = String(Int(difference!) / 60 % 60) let time = "\(hours)h\(minutes)m" return time }if someone have an idea how do that simply et properly thank you !
最满意答案
你可以做这样的事情
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = String(Int(difference!) / 3600) if Int(hours)! < 10 { hours = "0\(hours)" } var minutes = String(Int(difference!) / 60 % 60) if Int(minutes)! < 10 { minutes = "0\(minutes)" } let time = "\(hours)h\(minutes)m" return time }或者更好的方式
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = Int(difference!) / 3600 let minutes = Int(difference!) / 60 % 60 let time = "\(String(format: "%02d", hours))h\(String(format: "%02d", minutes))m" return time }建议在这里 - 在Swift中Int的前导零
You can do something like this
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = String(Int(difference!) / 3600) if Int(hours)! < 10 { hours = "0\(hours)" } var minutes = String(Int(difference!) / 60 % 60) if Int(minutes)! < 10 { minutes = "0\(minutes)" } let time = "\(hours)h\(minutes)m" return time }or a better way
class func getTime(_ user: SUser) -> String { let currentDate = Date() let firstDate = user.firstDate let difference = firstDate?.timeIntervalSince(now) let hours = Int(difference!) / 3600 let minutes = Int(difference!) / 60 % 60 let time = "\(String(format: "%02d", hours))h\(String(format: "%02d", minutes))m" return time }Suggested here - Leading zeros for Int in Swift
更多推荐
发布评论