Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift Dateformatter() converts wrong date

I want to convert a string into a date and use a small function. If I run the code the results is different from the input:

 let startDateString = "20-02-2022-15-00"
        print(startDateString)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "DD-MM-YYYY-HH:mm"
        dateFormatter.timeZone = TimeZone.current
        let startDate = dateFormatter.date(from: startDateString)
        print(startDate!)

the startDateString is "20-02-2022-15-00"
the result of startDate is: 2021-12-27 14:00:00 +0000

The results has the wrong format YYYY-MM-DD HH:MM vs DD-MM-YYYY-HH:mm
and the date is about two month in the past.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong?

>Solution :

You need yyyy not YYYY

dateFormatter.dateFormat = "dd-MM-yyyy-HH:mm"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading