簡単にやるには NSDateFormatter
の dateStyle
に指定するのが一番早い。
しかし年は不要、短い表記で曜日が欲しい等、もう少しカスタマイズして表示したい場合は dateFormatFromTemplate:options:locale
を使用すれば、ロケールと必要なフォーマット文字列を元に適切なフォーマット文字列を返してくれる。
func printDate( locale: NSLocale){
if let formatString = NSDateFormatter.dateFormatFromTemplate(
"MMMddE",
options: 0,
locale: locale ){
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = formatString
dateFormatter.locale = locale
let formattedDateString = dateFormatter.stringFromDate(NSDate())
print(formattedDateString)
}
}
// 日本
let jpLocale = NSLocale.init(localeIdentifier: "ja_JP")
printDate(jpLocale) // -> "1月13日(水)"
// US
let usLocale = NSLocale.init(localeIdentifier: "en_US")
printDate(usLocale) // -> "Wed, Jan 13"
// イギリス
let gbLocale = NSLocale.init(localeIdentifier: "en_GB")
printDate(gbLocale) // -> "Wed 13 Jan"
// カレントのロケール
let currentLocale = NSLocale.currentLocale()
printDate(currentLocale) // -> "Wed, Jan 13"