How to handle 12 hours Format?



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 = 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?

Powered by WPeMatico

You may also like...

Comments are closed.