How do I convert the time duration from DatePicker into a double to use in a calculation?

I am very new to SwiftUI and writing an iOS app to have the user enter start and end times (using DatePicker) and then extract the time difference into a double which can then be used to calculate the hourly work rate using a formula. So I need diffTime to be converted to a double and enter into a formula that spits out the hourly rate in US dollars.

I think I've called the correct functions, but don't know where to go from here.

I only need hours and minutes to calculate my work rates. I would be preferable to enter times in military time, but don't know how to do that either.

Below is my code so far:

import SwiftUI

struct ContentView: View {       @State private var startTime = Date()   @State private var endTime = Date()       var body: some View {     NavigationView {       Form {         Section(header: Text("Enter Case Times:")) {           DatePicker("Start Time", selection: $startTime , displayedComponents: .hourAndMinute)           DatePicker("End Time", selection: $endTime, displayedComponents: .hourAndMinute)         }         Section(header: Text("Case Duration:")) {           let diffTime = DateInterval.init(start: startTime, end: endTime)         }       }       .navigationTitle("Rate Calculator")     }   } }

You're new to the forum, so welcome.

When you paste code, please use Paste and match Style, then use the code formatter ( |<>]button.

import SwiftUI

struct ContentView: View {
    @State private var startTime = Date()
    @State private var endTime = Date()
    
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Enter Case Times:")) {
                    DatePicker("Start Time", selection: $startTime , displayedComponents: .hourAndMinute)
                    DatePicker("End Time", selection: $endTime, displayedComponents: .hourAndMinute)
                }
                Section(header: Text("Case Duration:")) {
                    let diffTime = DateInterval.init(start: startTime, end: endTime)
                }
            }
            .navigationTitle("Rate Calculator")
        }
    }
}

Your diffTime is just a timeInterval, not a value.

You could use instead:

                    let diffTime = endTime.timeIntervalSince1970 - startTime.timeIntervalSince1970

And get a number of seconds.

Or to just have an interger:

                    let diffTime = Int(endTime.timeIntervalSince1970 - startTime.timeIntervalSince1970)

To get hours and minutes:

                    let diffTime = Int(endTime.timeIntervalSince1970 - startTime.timeIntervalSince1970)
                    let minutes = (diffTime % 3600) / 60
                    let hours = diffTime / 3600
                    Text("\(diffTime) s: \(hours) h:\(minutes) m")

Take care if endDate is the next day…

Yes, that worked!

But what do you mean by "Your diffTime is just a timeInterval, not a value?" What exactly is an interval? Also, the declaration for this function is "var timeIntervalSince1970: TimeInterval { get }" but what does that mean? Is it an integer or a double? I am assuming an integer since is says "interval" in the name. But how do we know exactly what type these calls return when they are vague like that?

Also, where can I better search for these functions? Currently I'm just using the developer.apple/com and typing things I might be interested in inside the search bar.

Also, a followup question ...

How can I round this time duration UP to the nearest 15 minute interval and print it out to check?

After playing around with this for a bit I realize it doesn't quite work correctly all the time. It seems to be off in the calculation of the total difference (or duration) of time by several seconds, which doesn't show the exact hours and minutes correctly.

Is there another way to do this?

I tried using two different time interval functions to calculate time intervals and they both do not appear to be working as expected. Notice how they seem to underestimate the time difference by a fair amount of seconds so that the minutes and seconds are off when I isolate. them. What am I doing wrong?

Also, please show me how I can round the time up to the nearest 15 minutes.

Below is my code below to understand what I am experiencing.

import SwiftUI

struct ContentView: View {

@State private var startTime = Date()
@State private var endTime = Date()

var body: some View {
    NavigationView {
        
        Form {
            Section(header: Text("Enter Case Times:")) {
                DatePicker("Start Time", selection: $startTime , displayedComponents: .hourAndMinute)
                DatePicker("End Time", selection: $endTime, in: startTime..., displayedComponents: .hourAndMinute)
            }
            Section(header: Text("Case Duration:")) {
                let diffTime = Int(endTime.timeIntervalSince1970 - startTime.timeIntervalSince1970)
                let caseHours = diffTime / 3600
                let caseMinutes = (diffTime % 3600) / 60
                Text("\(diffTime) seconds =  \(caseHours) h:\(caseMinutes) m")
                
                let interval = DateInterval(start: startTime, end: endTime)
                Text("duration = \(interval.duration)")
                let durationHours = interval.duration / 3600
                let durationMinutes =
                (interval.duration.truncatingRemainder(dividingBy: 3600)) / 60
                Text("\(durationHours) hrs: \(durationMinutes) m")
                
            }
        }
        .navigationTitle("DDA Rates Calculator")
    }
}

}

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

How do I convert the time duration from DatePicker into a double to use in a calculation?
 
 
Q