To get the current time in Swift:
- Choose a date.
- Grab the hour, minute, and the second using calendar’s time components.
- Display the time by grouping the time components.
For example, let’s show what time it is right now in the console:
import Foundation // 1. Choose a date let today = Date() // 2. Pick the date components let hours = (Calendar.current.component(.hour, from: today)) let minutes = (Calendar.current.component(.minute, from: today)) let seconds = (Calendar.current.component(.second, from: today)) // 3. Show the time print("\(hours):\(minutes):\(seconds)")
Thanks for reading. I hope you find it useful.