0

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()
    }


5
  • What's your TimeZone? Are you at 6h diff from UTC? Commented Jun 23, 2023 at 14:33
  • Could you check with print(final); print(final.description(with: .current)) what's the output for the second one? Commented Jun 23, 2023 at 14:35
  • Unrelated but why aren’t you using the variable utcTime directly? Commented Jun 23, 2023 at 14:53
  • 1
    Regarding the strange date, year is represented using lowercase y so use “dd/MM/yyyy” Commented Jun 23, 2023 at 15:48
  • yess.. the issue of the wrong date was the YYYY , thanks a lot Commented Jun 24, 2023 at 2:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.