Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift - custom local date formatting

Using swift, I'd like my custom dateFormatter.dateFormat to be MMM-d or d-MMM depending on user's location. It seems easy enough if I used a default short.medium.etc style, but for design/layout considerations I need this custom format.

Any help?

like image 506
Shane Avatar asked Jul 26 '15 00:07

Shane


3 Answers

You could read the current device locale and set the format accordingly.

var dateFormat: String
switch NSLocale.currentLocale().localeIdentifier {
    case "en_US": dateFormat = "MMM d"
    ...
    default: dateFormat = "d MMM"
}

Also take a look at NSDateFormatter.dateFormatFromTemplate:

NSDateFormatter.dateFormatFromTemplate("MMM dd", options: 0, locale: NSLocale.currentLocale())

Which returns format and order applicable for given locale consisting of the elements you specify (month and day in this case), but it isn't always "MMM d" or "d MMM" as you require. You can run this to see the strings it actually generates for each locale:

let formatter: DateFormatter = DateFormatter()
for id in NSLocale.availableLocaleIdentifiers {
  let locale = NSLocale(localeIdentifier: id)
  let format = DateFormatter.dateFormat(fromTemplate: "MMM dd", options: 0, locale: locale as Locale) ?? "n/a"
  formatter.dateFormat = format
  formatter.locale = locale as Locale!
  let fd = formatter.string(from: NSDate() as Date)
  print("\(id)\t\(format)\t\(fd)")
}
like image 53
MirekE Avatar answered Nov 15 '22 06:11

MirekE


This is old question, but I found it by doing hard research. It's been a while and Swift 3 is out. So I converted the @sagits answer to Swift 3 if anyone needs it:

let myDate = "2017-08-19 13:52:58"

static func getFormatedDateTime(sqlDate: String?) -> String {

    if (sqlDate == nil || sqlDate == "") {
        return ""
    }

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH-mm-ss"
    let date = dateFormatter.date(from: sqlDate!)

    let localFormatter = DateFormatter.dateFormat(fromTemplate: "yyyy/MM/dd HH-mm", options: 0, locale: NSLocale.current)

    dateFormatter.dateFormat = localFormatter


    let outputDate = dateFormatter.string(from: date!)

    return outputDate
}

//Call the function out
getFormatedDateTime(sqlDate: myDate)
like image 45
Tarvo Mäesepp Avatar answered Nov 15 '22 07:11

Tarvo Mäesepp


Above answer when using an sql datetime from server:

static func getFormatedDateTime(var sqlDate : String?) -> String {

    if (sqlDate == nil || sqlDate == "") {
        return "";
    }

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH-mm-ss"
    var date = dateFormatter.dateFromString(sqlDate!) as NSDate!

    let localFormatter = NSDateFormatter.dateFormatFromTemplate("yyyy/MM/dd HH-mm", options: 0, locale: NSLocale.currentLocale())

    dateFormatter.dateFormat = localFormatter

    let outputDate = dateFormatter.stringFromDate(date)

    return outputDate
}
like image 44
Renato Probst Avatar answered Nov 15 '22 08:11

Renato Probst