我认为你需要看一下:
+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale
根据文档:
返回本地化日期格式字符串,表示为指定区域设置适当排列的给定日期格式组件。
返回值
本地化的日期格式字符串,表示模板中给定的日期格式组件,根据区域设置指定的区域设置进行适当排列。
返回的字符串可能不完全包含模板中给出的那些组件,但可能(例如)应用了特定于区域设置的调整。
讨论
不同的语言环境对于日期组件的排序有不同的约定。您可以使用此方法为指定区域设置的给定组件集获取适当的格式字符串(通常您使用当前区域设置 - 请参阅 currentLocale)。
以下示例显示了英式英语和美式英语的日期格式之间的差异:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *dateFormat;
// NOTE!!! I removed the 'y' from the example
NSString *dateComponents = @"MMMMd"; //@"yMMMMd";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
[usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
[gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y
额外代码(将其添加到上面的代码中):
//
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.locale = gbLocale;
formatter.dateFormat = dateFormat;
NSLog(@"date: %@", [formatter stringFromDate: [NSDate date]]);
看这里:NSDateFormatter 类参考 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options:locale: