1

On iOS 11+ only, I have this bug : When user has disabled 24h mode in phone hour settings, my string can't be parse to date using this code.

On other iOS version, there is no problems...

Is something missing to force 24h mode for the formatter ?

Variables date and beginHour came from webservice.

// input date = 20171201
// input beginHour = 2359

let dateTimeFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.timeZone = TimeZone.autoupdatingCurrent
dateTimeFormatter.dateFormat = "yyyyMMdd HHmm"

if let date = dateTimeFormatter.date(from: date+" "+beginHour) {
    // ...
}
Florian Mac Langlade
  • 1,863
  • 7
  • 28
  • 57
  • 2
    Most probably a duplicate of https://stackoverflow.com/questions/40692378/dateformatter-doesnt-return-date-for-hhmmss  – set the formatters locale to "en_US_POSIX" when parsing fixed-format strings. – Martin R Jan 04 '18 at 15:20
  • Already tried, do not works. – Florian Mac Langlade Jan 04 '18 at 15:26
  • 1
    That is strange. Can you show your updated code? (And please copy your *real* code, there is an inconsistency "dateTimeFormatter" vs "dateFormatter" in the question.) – Martin R Jan 04 '18 at 15:29
  • it working in 11.0.3 : tested in 24hrs disable mode let dateTimeFormatter = DateFormatter() dateTimeFormatter.locale = Locale(identifier: "fr_FR") dateTimeFormatter.timeZone = TimeZone.autoupdatingCurrent dateTimeFormatter.dateFormat = "yyyyMMdd HHmm" if let date = dateTimeFormatter.date(from: "20171201"+" "+"2359") { print(date) } output: 2017-12-01 18:29:00 +0000 – MAhipal Singh Jan 04 '18 at 15:29
  • That was a variable name error... Thank you @Martin R – Florian Mac Langlade Jan 04 '18 at 15:40
  • try this Post:- https://stackoverflow.com/a/47942040/6822622 – AshvinGudaliya Jan 04 '18 at 18:05

1 Answers1

1

I think in your code, you are not using the right dateFormatter, kindly check between dateTimeFormatter and dateFormatter,

let dateTimeFormatter = DateFormatter()
dateTimeFormatter.locale = Locale(identifier: "fr_FR")
dateTimeFormatter.timeZone = TimeZone.autoupdatingCurrent
dateTimeFormatter.dateFormat = "yyyyMMdd HHmm"

if let date = dateTimeFormatter.date(from: date+" "+beginHour) {
    // ...
}
Madhur
  • 1,056
  • 9
  • 14