我想在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)
}

您可以尝试以下操作:

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))

最新更新