I would like to display only the day and the month of a date, but I want it based on the user's locale.
For example I have the following date: 21/05/2015 00:16:00 GMT+10
I want to have May 21
if the locale is en_US
or 21 May
if the locale if fr_FR
for example.
I looked with the dateStyle
of NSDateFormatter
formatter but couldn't find what I want.
Something along these lines, perhaps:
let d = // the date
let df = NSDateFormatter()
let format = NSDateFormatter.dateFormatFromTemplate(
"dMMMM", options:0, locale:NSLocale.currentLocale())
df.dateFormat = format
let s = df.stringFromDate(d)
Note that both language and region settings on the device are involved in the outcome.
You can use simple extension for it:
public extension NSDate {
func getNiceDate() -> String! {
let dateFormatter = NSDateFormatter()
let format = NSDateFormatter.dateFormatFromTemplate(
"dMMMM", options:0, locale:NSLocale(localeIdentifier: "en_US"))
dateFormatter.dateFormat = format
return dateFormatter.stringFromDate(self)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With