Learn Courses My Dashboard

Date Formatter Not Working

I am working with dates in swift , like I have to convert from string to data , but some weird stuff is happening when I run me code on emulator and pass my date string to formatter , it return nil , but when i run my code on physical device , it works perfectly:

“08-Jan-2023 at 8:57:10 PM GMT+5” (my string date)

this static member is my formatter and I am using it like

let date = ChatViewController.dateFromatter.date(from: dateString)

please help me why this is happening

If it works on a real device, I’d lost likely say it works and erase your simulator settings and reset it

Also dateFormatter doesn’t need to be public static, and a better way to do that would to be extending DateFormatter itself or extending Date

Sir I just got the solution , actually the the problem was of locale . The locale of emulator was different than as of physical device , so t the string format in emulator and on physical device was different . So what I did was just hardcoded the format, like I set the property as
formatter.dateFormat = “dd-MMM-yyyy ‘at’ h:mm:ss a Z” and it worked
At last …!

and also this property
formatter.locale = Locale(identifier: “en_US_POSIX”)