I've inserted an timer, so the time changes according to the system clock. the problem is, its displaying in numbers without am/pm like(12:33). please guide me to solve this problem.
This is the code I used to display the text.
@State var endTime = Date()
var body: some View {
Text("Ends - \(myViewModel.timeString(date: endTime))")
.onAppear(perform: {let _ = self.updatedEndTime})
}
This is code I used to update timer according to the system time.
var updateEndTime: Timer {
Timer.scheduledTimer(withTimeInterval: 0, repeats: true,
block: {_ in
self.endTime = Date() + myViewModel.player.duration - myViewModel.player.currentTime
})
}
My view model code
var timeFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm"
return formatter
}
func timeString(date: Date) -> String {
let time = timeFormat.string(from: date)
return time
}