ios - 按下按钮时显示一天中的时间 ios

标签 ios swift swift3

我需要显示一天中的时间(例如,14:02)并将其显示在 UILable 中。

这是我检索系统时间的代码:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .nanosecond], from: date)
 _ = components.hour

我不知道如何将它提供给标签。

mylable.text = "???"

谢谢。

最佳答案

您需要使用 DateFormatter而不是 DateComponents得到DateTime以特定格式。

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let time = dateFormatter.string(from: Date())
mylable.text = time

关于ios - 按下按钮时显示一天中的时间 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956669/

相关文章:

ios - 如何在 cellForRowAtIndexPath 处暂停

ios - UIWebView无法加载本地网页

iOS开发应用程序启动崩溃

ios语音识别错误域=kAFAssistantErrorDomain代码=216 "(null)"

ios - 将 Dictionary 转换为 Json 以在 Alamofire 中传递参数

ios - 重用之前设置为 inputAccessoryView 的 UIView 似乎不起作用

ios - 在 iOS 中为每个级别的游戏保存高分

ios - 登录 iOS 应用程序和使用 Rails 设计后的 session 保留

swift - 如何从另一个 Controller 中解除 WKInterfaceController?

ios - 如何从推送通知响应 Swift 3/iOS 获取数据