Search code examples
iosswiftuitextfieldnsdateformatter

I'm trying to calculate the difference between two times (HH:MM:SS) in iOS


I have two UIButtons that mark the time in two different UITextFields. I have a UIButton to calculate on the action to determine the difference.

Here is my code:

class ViewController: UIViewController {

@IBOutlet weak var timeatf1TextField: UITextField!
@IBOutlet weak var timeatf2TextField: UITextField!

@IBOutlet weak var timediffTextField: UITextField!

@IBAction func markf1Button(_ sender: Any) {
    let now = Date()
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "HH:MM:SS"
    let dateString = formatter.string(from: now)
    timeatf1TextField.text = dateString
}

@IBAction func markf2Button(_ sender: Any) {
    let now = Date()
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = "HH:MM:SS"
    let dateString = formatter.string(from: now)
    timeatf2TextField.text = dateString
}
    timediffTextField.text = "\((Double(timeatf2TextField.text ??     
    "0.0") ?? 0.0) - (Double(timeatf1TextField.text ?? "0.0") ?? 0.0))"

The result I get on the timediffTextField.text action is 0.0. I appreciate any help here!


Solution

  • timeatf1TextField and timeatf2TextField don't contain doubles, they contain Strings representing the formatted time.

    I would, instead, have two properties, which were types of Date which mark the start and times. When the appropriate markXxx function is called, you would set the Date to the appropriate property (like you are doing, but to a property rather then local variable)

    This will mean you don't need to parse the String values back to a Date value

    Then I'd use Date#timeIntervalSince(Date) to calculate the difference between these values

    Also, the date format you probably want to be using is HH:mm:ss (MM is month)

    @IBOutlet weak var timeatf1TextField: UITextField!
    @IBOutlet weak var timeatf2TextField: UITextField!
    
    @IBOutlet weak var timediffTextField: UITextField!
    
    var dateFormat: DateFormater = {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone.current
        formatter.dateFormat = "HH:mm:ss"
    }()
    
    // Yes, this could be an array
    var mark1: Date?
    var mark2: Date?
    
    @IBAction func markf1Button(_ sender: Any) {
        mark1 = Date()
        let dateString = dateFormat.string(from: mark1 ?? Date())
        timeatf1TextField.text = dateString
    }
    
    @IBAction func markf2Button(_ sender: Any) {
        mark2 = Date()
        let dateString = dateFormat.string(from: mark2 ?? Date())
        timeatf2TextField.text = dateString
    }
    

    Then when required...

    guard let mark1 = mark1, let mark2 = mark2 else {
        return
    }
    let time: TimeInterval = mark2.timeIntervalSince(mark1)
    

    You could use something like DateComponentsFormatter to format the resulting duration into something human readable