Search code examples
iosswiftdateformatter

getting a nil value while calculating time slots swift


I'm following the solution in post how to get 30 minutes time slots array in between two dates swift but I get a nil value on date2in my following function:

func calculateOpenTimeSlots() {

        let formatter = DateFormatter()
        formatter.dateFormat = "hh:mm"
        let weekday = self.actualWeekday
        let openingTime = openingTimeArray[weekday!].openingTime
        let closingTime = openingTimeArray[weekday!].closingTime
        let date1 = formatter.date(from: openingTime)
        let date2 = formatter.date(from: closingTime)
        var i = 1
        timeSlotArray.removeAll()
        while true {
            let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
            let string = formatter.string(from: date!)
            if date! >= date2! {break}
            i = i + 1
            timeSlotArray.append(string)
        }

    }

the array is:

var openingTimeArray: [(weekday: Int, openingTime: String, closingTime: String)] = [(weekday: 0, openingTime: "10:00", closingTime: "19:00"), (weekday: 1, openingTime: "10:00", closingTime: "19:00"), (weekday: 2, openingTime: "10:00", closingTime: "19:00"), (weekday: 3, openingTime: "10:00", closingTime: "19:00"), (weekday: 4, openingTime: "10:00", closingTime: "19:00"), (weekday: 5, openingTime: "10:00", closingTime: "19:00"), (weekday: 6, openingTime: "10:00", closingTime: "19:00"), (weekday: 7, openingTime: "10:00", closingTime: "19:00")]

openingTime and closingTimevalues are not nil and they are the right value, but on date1I get a complete date with hour being 1 hour early. date1 is 2000-01-01 09:00:00 UTC but I expected it to be 10:00.

I still don't master date formatter and I don't understand why I get a nil date2and a wrong date1. Any explanation to point me in the right direction will be very helpful as usual. Many thanks


Solution

  • I found out that the problem was in the format declaration being formatter.dateFormat = "hh:mm". I changed it to formatter.dateFormat = "HH:mm"and it now works as expected. I had to add an append timeSlotArray.append(openingTime)before the whileloop because the loop was not appending the first time slot starting at opening time, but I would like to rewrite the loop to take that into consideration. Any suggestion on how to achieve that?

    func calculateOpenTimeSlots() {
             timeSlotArray.removeAll()
    
            let formatter = DateFormatter()
    //        formatter.timeZone = TimeZone(identifier: "UTC")
            formatter.dateFormat = "HH:mm"
            let weekday = self.actualWeekday
            let openingTime = openingTimeArray[weekday!].openingTime
            let closingTime = openingTimeArray[weekday!].closingTime
            let date1 = formatter.date(from: openingTime)
            timeSlotArray.append(openingTime)
            let date2 = formatter.date(from: closingTime)
            var i = 1
    
            while true {
                let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
                let string = formatter.string(from: date!)
                if date! >= date2! {break}
                i = i + 1
                timeSlotArray.append(string)
            }
            print(timeSlotArray)
    
        }