NSDateFormatter Locale

How to install Locale for NSDateFormatter? I tried the code below and did not work.

- (NSString *)localizedStringWithFormat:(NSString *)format {

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:format];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"fr_FR"];
    cal.locale = locale;
    df.calendar = cal;
    return [df stringFromDate:self];
}

Please let me know how to do this.

Thanks.

+3
source share
4 answers
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] 
                     initWithLocaleIdentifier:@"he"];
[dateFormatter setLocale:locale];
+10
source

A bit late for the party, but this is what I did today in Swift:

let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
df.timeStyle = .MediumStyle
df.dateStyle = .MediumStyle
println("Date: \(df.stringFromDate(obj.dateSent!))")

Depending on the settings of your OS area, the output should look like this (I'm in Germany):

Date: 27.11.2014 12:30:39

Note. I just thought that a lot more people are stumbling over this question, so I think that it can’t hurt to answer.

+2
source

, .

From apple docs: Use the locale if you don't need localizations . Use the current language to format the text that you display to users.

Code: [formatter setLocale: [NSLocale systemLocale]];

+1
source

if someone is looking for a Swift 3 solution :

let dateFormatter = DateFormatter()
let frLocale = Locale(identifier: "fr")
dateFormatter.locale = frLocale
0
source

Source: https://habr.com/ru/post/1629000/


All Articles