簡體   English   中英

如何使用UILabel和DateFormatter顯示當前時間?

[英]How to use UILabel and DateFormatter to display the current time?

每當用戶啟動APP時,我都試圖使用UILabel和DateFormatter來顯示時間(日期,分鍾,秒)。

我目前從stackoverflow找到了

let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"

但似乎我必須花一點時間進入格式化程序。 如何只讓UILabel顯示當前時間而不是固定時間?

label.text = dateFormatter.string(from: Date())

如果要顯示當前時間而不傳遞任何變量,請執行以下操作:

let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"

let exactlyCurrentTime: Date = Date()
print(dateFormatterPrint.stringFromDate(exactlyCurrentTime))

// e.g Set your label text:
myLabel.text = "Current Time: \(dateFormatterPrint.stringFromDate(exactlyCurrentTime))"

例如,如果我們想要: Friday, Nov 16, 2018 | 我們設置: EEEE, MMM d, yyyy

我也建議您訪問NSDateFormatter.com,以了解dateFormat工作原理。

祝你好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM