Andrey
Andrey

Reputation: 2729

Swift converting String to NSDate returns nil

I'm fighting with String to NSDate conversion in Swift. I hope someone could help me with it.

my code:

let formatter = NSDateFormatter()

formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(name: "UTC")
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
formatter.formatterBehavior = .BehaviorDefault

let tripDate = formatter.dateFromString(updatedAt)

Variable updatedAt is "2015-11-11 15:43:18"

After execution tripDate is nil.

What could be wrong?

Thanks.

Upvotes: 2

Views: 205

Answers (1)

Eric Aya
Eric Aya

Reputation: 70097

You need to use HH instead of hh.

hh is for the 12h system with AM and PM, HH is for the 24h system.

Example:

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

Upvotes: 6

Related Questions