I’ve created a swiftui form with DatePickers for a startTime and an EndTime. I want to display the total and update it from an empty string to the hour and minute (example 10hrs 30min).
@State private var startTime = Date()
@State private var endTime = Date()
@State private var totalTime = differenceBetweenStartAndEndTimes
Form {
DatePicker("Start Time", selection: $startTime, displayedComponents: .hourAndMinute)
DatePicker("End Time", selection: $endTime, displayedComponents: .hourAndMinute)
Text("Total: (totalTime)")
}
I tried using Date().offset(from: endTime, to: startTime) and got errors back.
Then I tried this piece of code from stackOverflow:
func differenceBetweenStartAndEndTimes() {
let cal = Calendar.current
let components = cal.dateComponents([.hour], from: endTime, to: startTime)
let diff = components.hour!
}
still can’t get it to work… I just started learning how to code with no previous knowledge 2 months ago so it may be something super simple I’m just not thinking about!!
2
Answers
I think you really want to use a date formatter and a time interval. See the comments in the code, but you are trying to hard to make a variable have what you want to display. You can simply cause the display itself to format it the way you want. All you need to save is the actual time interval.
This is SwiftUI, in SwiftUI 3 there are a lot of convenient native formatters if you can live with
hr
rather thanhrs