#swift #swiftui
#swift #swiftui
Вопрос:
Я хочу извлечь разницу между двумя временами в swiftui
между временем ожидания и временем :
Section(header: Text("Date and time")) {
HStack {
DatePicker("Date", selection: $todayDate, displayedComponents: .date)
} .datePickerStyle(GraphicalDatePickerStyle())
HStack {
DatePicker("Time Out", selection: $inTime, displayedComponents: .hourAndMinute)
}
HStack {
DatePicker("Time In", selection: $outTime, displayedComponents: .hourAndMinute)
}
// Here I want to write the difference between the two times in the hour and the minute automatically ... ( Like : your permission is [1:00] or [1:30])
Text("Here is the difference between the two times")
.foregroundColor(Color.red)
.lineLimit(2)
}
Комментарии:
1. Посмотрите на DateComponentsFormatter
Ответ №1:
вы могли бы попробовать что-то вроде этого:
func timeDiff(_ t1: Date, _ t2: Date) -> String {
let m1 = Calendar.current.component(.minute, from: t1)
let h1 = Calendar.current.component(.hour, from: t1)
let m2 = Calendar.current.component(.minute, from: t2)
let h2 = Calendar.current.component(.hour, from: t2)
// adjust the values as you see fit
return "your permission is [(abs(h2-h1)):(abs(m2-m1))]"
}
....
Text(timeDiff(inTime, outTime))