Search code examples
iosswiftdatepickernsdate

Adding time to date picker date and time results in same date and time. SWIFT


I have a datePickerthat I use to select a starting date and time, and a durationTextLabelto add minutes to that date and time. I set the duration to be minimum 30 if no text is inserted, but the value in resulting date and time is identical. Can you see where I'm mistaking? Thank you very much as usual.

Here's the function:

func setQueryParameters() {
        let dateFormatter = DateFormatter()
        var convertedDate: String!

        dateFormatter.dateFormat = "yyyy/MM/dd/hh/mm"
        convertedDate = dateFormatter.string(from: datePicker.date)
        let calendar = Calendar.current

        let components = (calendar as NSCalendar).components([.year, .month, .day, .weekday, .hour, .minute] , from: datePicker.date)
        let year: Int = components.year!
        let month: Int = components.month!
        let day: Int = components.day!
        let weekday: Int = components.weekday!
        let hour: Int = components.hour!
        let minute: Int = components.minute!

        var duration: Double?
        duration = Double(durationTextField.text!)
        let endDate = datePicker.date.addingTimeInterval(duration!)
        let endComponents = (calendar as NSCalendar).components([.hour, .minute], from: endDate)
        let endHour: Int = endComponents.hour!
        let endMinute: Int = endComponents.minute!

        if durationTextField.text != nil {
            duration = Double(durationTextField.text!) ?? 30.00

        } else { return}

        // Opening Time Query parameter
        openingTimeQueryStart = Int("\(String(describing: weekday))"+"00"+"00")!
        openingTimeQueryEnd = Int("\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))")!
        print("opening query is \(openingTimeQueryEnd)")



        // Closing Time Query parameter
        closingTimeQueryStart = Int("\(String(describing: weekday))"+"\(String(endHour))"+"\(String(endMinute))")!
        closingTimeQueryEnd = Int("\(String(describing: weekday))"+"00"+"00")!


        print("closing time query is \(closingTimeQueryStart)")


        // Booking Query parameter
        let bookingQueryString = "\(String(describing: year))"+"\(String(describing: month))"+"\(String(describing: day))"+"\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))"+"\(String(endHour))"+"\(String(endMinute))"

        bookingQuery = Int(bookingQueryString)!// ?? openingTimeQuery  // found nil unwripping optional

    }

Solution

  • There are many problems here.

    1. You actually never make any use of dateFormatter other than creating and then never using convertedDate. So delete that unused code.
    2. You have indicated at duration should be in minutes but you treat it as seconds. You need to multiply by 60 to convert it to minutes.
    3. All of your code for calculating things such as openingTimeQueryEnd depend on each value being two digits but your code doesn't give the desired results.

    For example, the line:

    openingTimeQueryEnd = Int("\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))")!
    

    should be rewritten as:

    openingTimeQueryEnd = Int(String(format: "%02d%02d%02d", weekday, hour, minute))!
    

    or as:

    openingTimeQueryEnd = weekday * 10000 + hour * 100 + minute
    

    Make similar changes to the other similar lines.