Reputation: 3697
Sorry if this has been asked before.
I have a date from MySQL populating a tableview in Swift. I am converting the MySQL date to NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = "\(dateLabelSource)"
if let dateAdded = dateFormatter.dateFromString(dateString)
{
self.dateLabel.text = "\(dateAdded)"
}
This produces the correct date but now I need to change the output to read more like:
Monday 7 September 2015
but can't work out how to do this.
Any help is appreciated.
Upvotes: 0
Views: 255
Reputation: 1475
Use this code to format date:
let dateFmt: NSDateFormatter = NSDateFormatter()
dateFmt.dateFormat = "EEEE d MMMM yyyy"
dateFmt.locale = NSLocale(localeIdentifier: "en_US") // If you consider to not localise date
println(dateFmt.stringFromDate(dateAdded))
Upvotes: 0
Reputation: 1
This will Help
var formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "EEEE d MMMM yyyy"
let stringDate: String = formatter.stringFromDate(NSDate())
println(stringDate)
Upvotes: 0
Reputation: 3863
This should produce the output you want. Note: NSDateFormatters are expensive objects to create, so it is best to reuse a single instance as much as you can.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = "\(dateLabelSource)"
if let dateAdded = dateFormatter.dateFromString(dateString)
{
dateFormatter.dateFormat = "EEEE d MMMM yyyy"
self.dateLabel.text = "\(dateFormatter.stringFromDate(dateAdded))"
}
The Data Formatting Guide has a section dealing with formatting dates. Data Formatting Guide
Upvotes: 2