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?

GDev
  • 91
  • 4
  • 1
    As a work around. I have added this to my code. appStartDate = appStartDate.replacingOccurrences(of: "Sep", with: "Sept") – GDev Mar 20 '22 at 07:45
  • 4
    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? – vadian Mar 20 '22 at 07:45
  • 4
    Thanks vadain. used `dateFormatter.locale = Locale(identifier: "en_US_POSIX")` and it worked. Cheers. – GDev Mar 20 '22 at 07:52
  • 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 lol – Yessen Yermukhanbet Apr 15 '22 at 06:10
  • Got the same issue, think it's iOS15.4 SDK issue. – Matthew An Apr 26 '22 at 23:17
  • The same issue here. I think it's a bug on iOS 15.4 SDK – Eduardo Santi Apr 27 '22 at 13:58
  • 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()? – geohei Aug 29 '22 at 15:41

0 Answers0