How to handle 12 hours Format?

Hi,


My Code to get Local Time From UTC:


func getLocalTimeFromUTC(dateString: String) -> (String,String){

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

let dt = dateFormatter.date(from: dateString)

dateFormatter.timeZone = TimeZone.current

dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"

let finalDate = dateFormatter.string(from: dt!)

let finalDateArr = finalDate.components(separatedBy: " ")

return (finalDateArr[0] , finalDateArr[1])

}


If the time format is 24 hours in my iPhone, no problem in the above code.

But when i change the time format from 24 hours to 12 hours in my iPhone, dt is nil.

How to handle this?

Answered by Claude31 in 403830022

Did you set the locale ?

You should do it before dateFormat:

https://forums.developer.apple.com/message/402920#402920


Could this help also ?

https://forums.developer.apple.com/message/395218#395218

Accepted Answer

Did you set the locale ?

You should do it before dateFormat:

https://forums.developer.apple.com/message/402920#402920


Could this help also ?

https://forums.developer.apple.com/message/395218#395218

How to handle 12 hours Format?
 
 
Q