get time swift code example
Example 1: swift get current time
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
Example 2: swift get current hour
let hour = Calendar.current.component(.hour, from: Date())
Example 3: swift get current hour
let formatter = DateFormatter()
formatter.dateFormat = "hh a" // "a" prints "pm" or "am"
let hourString = formatter.string(from: Date()) // "12 AM"