Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在swiftui中提取两次之间的差异_Swift_Swiftui - Fatal编程技术网

我想在swiftui中提取两次之间的差异

我想在swiftui中提取两次之间的差异,swift,swiftui,Swift,Swiftui,我想在swiftui中提取两次之间的差异 在超时和超时之间: Section(header: Text("Date and time")) { HStack { DatePicker("Date", selection: $todayDate, displayedComponents: .date) } .datePickerStyle(

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