This is my code:
let currentDate = NSDate()
let usDateFormat = NSDateFormatter()
usDateFormat.dateFormat = NSDateFormatter.dateFormatFromTemplate("d MMMM y", options: 0, locale: NSLocale(localeIdentifier: "en-US"))
cmt.date = usDateFormat.stringFromDate(currentDate)
I was expecting to get "15 October 2015", but I got "oktober 15, 2015". The month is in swedish locale.
What have I done wrong? Both locale and format are wrong.
Try this:
let dateString = "2015-10-15"
let formater = NSDateFormatter()
formater.dateFormat = "yyyy-MM-dd"
print(dateString)
formater.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formater.dateFromString(dateString)
print(date)
Swift 3 Xcode 8
let dateString = "2015-10-15"
let formater = DateFormatter()
formater.dateFormat = "yyyy-MM-dd"
print(dateString)
formater.locale = Locale(identifier: "en_US_POSIX")
let date = formater.date(from: dateString)
print(date!)
I hope it helps.