Date format "dd-MMM-yyyy" for September now Sept. Not Sep so converting causes nil
Asked Answered
B

0

9

Updated iPad to iOS 15.4.

Now String to Date conversion using DateFormatter returns nil when stored month format "MMM" = Sep.

Works ok when MMM Month = Sept.

Did work in iOS 15.3 and earlier.

Issue is that I have dates created and stored as dd-Sep-2021.

    let dateFormat = "dd-MMM-yyyy"
    let dateFormatter = DateFormatter()
    dateFormatter.calendar = Calendar(identifier: .iso8601)
    dateFormatter.dateFormat = dateFormat
    let startDate = dateFormatter.date(from: appStartDate) //Returns nil if “11-Sep-2021” and crashes
    print(String(describing: startDate))

Did some investigation with this code and found that Sept works and Sep doesn't.

    let x = "-Sep-2021" // This returns nil
    //let x = "-Sept-2021" // This works
    var z: String = ""
    let i = 1...30
    for n in i {
        z = String(n) + x
        guard let y = dateFormatter.date(from: z) else {
            print(String(n)) // will print if nil
            continue
        }
        print(\(String(describing: y))) //will print if string converted to date
    }

All other 3 character month formats work.

Any idea if things have changed?

Bullhorn answered 20/3, 2022 at 7:35 Comment(7)
As a work around. I have added this to my code. appStartDate = appStartDate.replacingOccurrences(of: "Sep", with: "Sept")Bullhorn
It's highly recommended to set the Locale of the date formatter to en_US_POSIX when dealing with custom date formats. And did you try the .gregorian calendar too?Affianced
Thanks vadain. used dateFormatter.locale = Locale(identifier: "en_US_POSIX") and it worked. Cheers.Bullhorn
Were there any announcements by apple that it will be changed from iOS 15.4? Was so frustrated to find this out, after million of test debugging, without understanding why it was returning nil lolSurprisal
Got the same issue, think it's iOS15.4 SDK issue.Tabethatabib
The same issue here. I think it's a bug on iOS 15.4 SDKLiquidambar
Date Picker (the new iOS 14.0 design) also shows Sept i.s.o Sep in compact style. How can I change the date format of Date Picker like I do for a date using DateFormat()?Uboat

© 2022 - 2024 — McMap. All rights reserved.