Nasir Khan Nasir Khan - 1 year ago 203
Swift Question

How to get Date from String in swift 3?

I have save the timestamp in data base like:

"NSDate()" // which save Date like this: 2017-03-10 22:53:30 +0000

Now i need to get Date from this string, I have tried every format but it's keep returning nil.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss ZZZ"
dateFormatter.locale = Locale(identifier: "en_US")
print( "2017-03-10 22:53:30 +0000") ?? "No Value") // always return No Value

here i have tried so many formats e.g:
yyyy-MM-dd hh:mm:ss +zzzz
yyyy-MM-dd hh:mm:ss xx

Have seen many answers but no answer is working on this format of date "2017-03-10 22:53:30 +0000"

Answer Source

Military time (24-value hours) uses the capital 'H'. Try this for your formatting String:

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download