1

and first of all thanks in advance. I've been working with iOS/Swift for several years and this is the first time I've seen this strange behaviour. This is the code:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy - HH:mm"

let dat = Date()
let formated = dateFormatter.string(from: dat)
CrashlyticsBridge.log("newsresponse date2: \(formated)")
...

I've received 3 crashes from the same user in Fabric with the following log:

+[CrashlyticsBridge log:] line 16 $ newsresponse date2: 23/02/2018 - 77:15 a. m.

The app crashes due to the strange behaviour of the DateFormatter. How can it be possible? HH == 77?

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
  • You can try Out:- https://stackoverflow.com/a/47942040/6822622 – AshvinGudaliya Feb 23 '18 at 08:23
  • One thing you can try is setting a calendar: `dateFormatter.calendar = Calendar(identifier: Calendar.Identifier.gregorian)` - but I guess that's not the issue since the year seems correct... You could also try using `dateFormatter.dateStyle = .medium` + `dateFormatter.timeStyle = .medium` instead of your custom dateFormat. Maybe that is more robust. – d.felber Feb 23 '18 at 08:33
  • And how can you have am/pm in there? I can only suspect you have another log statement somewhere or something like that. – Rob Feb 23 '18 at 08:55
  • Nope @Rob. I don't know how 77 and a. m. are where they are :/ Indeed I have tested the code with several simulators, several devices and everything seems fine. – Luciano Rodríguez Feb 23 '18 at 09:10

1 Answers1

1

It can be related to Locale and Calendar. Set locale explicitly like:

dateFormatter.locale = Locale(identifier: "en_US_POSIX")

Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66