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) {
    // ...
}
6
  • 2
    Most probably a duplicate of stackoverflow.com/questions/40692378/… – set the formatters locale to "en_US_POSIX" when parsing fixed-format strings. Commented Jan 4, 2018 at 15:20
  • Already tried, do not works. Commented Jan 4, 2018 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.) Commented Jan 4, 2018 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 Commented Jan 4, 2018 at 15:29
  • That was a variable name error... Thank you @Martin R Commented Jan 4, 2018 at 15:40

1 Answer 1

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) {
    // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Which 'dateTimeFormatter' are you talking about . Can you explain ??
in his code, he might have misspelled the dateFormatter, in the very first line he is using dateTimeFormatter and in 2 and 3 he is using dateFormatter
Yes and no errors because before this code, I have a variable named dateFormatter...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.