ios - Date conversion is always nil in iPhone 24hr date format -


i trying convert string date working in 12hr date format. when switch device date format 24, returning nil. please check below code

    let formatter = dateformatter()     formatter.dateformat = "h:mm a"     let date = formatter.date(from: "6:00 am") 

according technical q&a qa1480:

if you're working fixed-format dates, should first set locale of date formatter appropriate fixed format. in cases best locale choose "en_us_posix", locale that's designed yield english results regardless of both user , system preferences.

so, prevent date being interpreted using device's regional settings need set locale property of dateformatter

let formatter = dateformatter() formatter.locale = locale(identifier: "en_us_posix") formatter.dateformat = "hh:mm a" let date = formatter.date(from: "6:00 am") 

note: it's important set locale before set dateformat


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -