let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let Tijd = "\(hour) : \(minutes)"
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let tijd = String(format:"%d:%02d", hour, minutes) // change to "%02d:%02d" if you also want the hour to be 2-digits.
1.使用DateFormatter。
let date = Date()
let df = DateFormatter()
df.dateFormat = "H:mm" // Use "HH:mm" if you also what the hour to be 2-digits
let tijd = df.string(from: date)
2条答案
按热度按时间2g32fytz1#
你有两个选择。
1.使用
String(format:)
1.使用
DateFormatter
。bwleehnv2#
SwiftUi的解决方案: