RealBadCoder RealBadCoder - 4 months ago 13
Swift Question

Date format gone wrong in swift

I need to set a date in medium format like: jul 17, 2016 4:30:00 PM,
but I get 07/17/2016 4:30:00 p.m.
Any help will be highly appreciated. Thanks in advance..
this is my code:

let dateFormatter = NSDateFormatter()

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle

let mydate = dateFormatter.stringFromDate(NSDate())

Answer

For me the code you are using dose exactly what you need.

In case you need you can achieve this by using MMM dd, yyyy h:mm a as format string for your formatter object. Code follows:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy h:mm a"
let mydate = dateFormatter.stringFromDate(NSDate())
// "Jul 17, 2016 11:48 AM"