Swift: NSDate formatting with strftime & localtime

Here is another example that uses NSDateFormatterStyle:

private func FormatDate(date:NSDate) -> String {
  let dateFormatter = NSDateFormatter()
  dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
  return dateFormatter.stringFromDate(date)
}

The output is formatted as, "January 1, 1990".

If you want to read more about the formatter and the different available styles, checkout, NSFormatter under NSDateFormatter section.


As the commentators @BryanChen and @JasonCoco said, use NSDateFormatter.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd 'at' h:mm a" // superset of OP's format
let str = dateFormatter.stringFromDate(NSDate())

A full description of the format strings is available in "Data Formatting Guide".