why this date formatter not working correctly?

41 views Asked by At

facing the following issue:

I'm try to create a date by adding 2 strings one with a date "23/06/2023" and one is the time "21:00"

why I'm getting this strange result:

see my comment,

var final : Date {
        print("\(utcTime)") // ----time is correct 12:00
        let h = utcTime.prefix(2)
        let m = utcTime.suffix(2)
        let date : String = selectedDate.toUTCString()
        let cdate : String = date + " " + "\(h):\(m)" //---- string date is correctly composed 23/06/2023 12:00
        print(cdate)
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/YYYY HH:mm"
        
        if let final = formatter.date(from: cdate){
            
            print(final) // why here i'm getting this strange date???  2022-12-19 04:00:00
            return final
        
        }
        
        return Date()
    }


0

There are 0 answers