I have the following code
var calendar = Calendar.current
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
calendar.timeZone = TimeZone(identifier: "UTC")!
let startHour = calendar.component(.hour, from: cell.startDate as Date)
let startMinutes = calendar.component(.minute, from: cell.endDate as Date)
let endHour = calendar.component(.hour, from: cell.endDate as Date)
let endMinute = calendar.component(.minute, from: cell.endDate as Date)
print("start hours = \(startHour) : \(startMinutes)")
print("end hours = \(endHour) : \(endMinute)")
let myFormatter = DateFormatter()
myFormatter.dateFormat = "HH:mm"
There are various ways to add the missing leading zero, however the best solution is using the
DateFormatter. You have only one problem with the formatter - setting the correct time zone.
// safer than using identifiers that are not actually standardised myFormatter.timeZone = TimeZone(secondsFromGMT: 0)