在10 [重复]下添加0到小时/分钟(add 0 to hours/minutes under 10 [duplicate])

编程入门 行业动态 更新时间:2024-10-25 11:27:26
在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 answers

i'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

更多推荐

本文发布于:2023-08-03 07:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时   add   duplicate   minutes   hours

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!