NSDateFormatter区域设置

aru*_*t21 5 iphone objective-c nsdateformatter ios

如何为NSDateFormatter设置Locale?我已经尝试了以下代码,但它不起作用.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

请让我知道如何使这项工作.

谢谢.

Nir*_*alp 12

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] 
                     initWithLocaleIdentifier:@"he"];
[dateFormatter setLocale:locale];
Run Code Online (Sandbox Code Playgroud)


Tun*_*Fam 5

如果有人会寻找Swift 3解决方案:

let dateFormatter = DateFormatter()
let frLocale = Locale(identifier: "fr")
dateFormatter.locale = frLocale
Run Code Online (Sandbox Code Playgroud)