mankee mankee - 2 years ago 108
Swift Question

String into NSDate Swift2

I obtain the following string from my JSON response :

2016-05-24 17:13:08

I use the following code to parse it into an

if let messagedate = onemessage["timestamp"] as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let date = dateFormatter.dateFromString(messagedate) = date

is of type
initialised like this :

class Message : NSObject {
var id: String?
var text: String?
var date: NSDate?
var sender: Friend?

The problem is that the
value is always
after this
condition executes.

Answer Source

Use "HH" instead of "hh" for dateFormatter.dateFormat.

"hh" is for AM/PM format, "HH" is for 24 hours format, and you have the latter: 17:13:08.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download