如何确定区域设置的日期格式是月/日还是日/月?

时间:2022-10-16 09:46:20

In my iPhone app, I'd like to be able to determine if the user's locale's date format is Month/Day (i.e. 1/5 for January fifth) or Day/Month (i.e. 5/1 for January fifth). I have a custom NSDateFormatter which does not use one of the basic formats such as NSDateFormatterShortStyle (11/23/37).

在我的iPhone应用程序中,我希望能够确定用户的区域设置的日期格式是月/日(即1月5日的1/5)还是日/月(即1月5日的5/1)。我有一个自定义NSDateFormatter,它不使用NSDateFormatterShortStyle(11/23/37)等基本格式之一。

In an ideal world, I want to use NSDateFormatterShortStyle, but just not display the year (only month & day#). What's the best way to accomplish this?

在理想的世界中,我想使用NSDateFormatterShortStyle,但只是不显示年份(只有月份和日期#)。实现这一目标的最佳方法是什么?

2 个解决方案

#1


18  

You want to use NSDateFormatter's +dateFormatFromTemplate:options:locale:

您想使用NSDateFormatter的+ dateFormatFromTemplate:options:locale:

Here is some Apple sample code:

这是一些Apple示例代码:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

NSString *dateFormat;
NSString *dateComponents = @"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

#2


5  

Building on the sample code, here's a one-liner to determine if the current locale is day-first (nb. I dropped the 'y' as that doesn't concern us for this question):

在示例代码的基础上,这里是一个单行程序来确定当前的语言环境是否为第一天(nb。我删除了'y',因为这与我们的问题无关):

BOOL dayFirst = [[NSDateFormatter dateFormatFromTemplate:@"MMMMd" options:0 locale:[NSLocale currentLocale]] hasPrefix:@"d"];

NB. The docs for dateFormatFromTemplate state that "The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied." given this, sometimes the test may return FALSE due to an unknown format (meaning it defaults to month-first when unclear). Decide for yourself which default you prefer, or if you need to enhance the test to support additional locales.

NB。 dateFormatFromTemplate的文档声明“返回的字符串可能不包含模板中给出的那些组件,但可能 - 例如 - 应用了特定于语言环境的调整。”鉴于此,有时由于格式未知,测试可能会返回FALSE(意味着默认情况下默认为月份优先)。确定您喜欢哪个默认值,或者您是否需要增强测试以支持其他语言环境。

#1


18  

You want to use NSDateFormatter's +dateFormatFromTemplate:options:locale:

您想使用NSDateFormatter的+ dateFormatFromTemplate:options:locale:

Here is some Apple sample code:

这是一些Apple示例代码:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

NSString *dateFormat;
NSString *dateComponents = @"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

#2


5  

Building on the sample code, here's a one-liner to determine if the current locale is day-first (nb. I dropped the 'y' as that doesn't concern us for this question):

在示例代码的基础上,这里是一个单行程序来确定当前的语言环境是否为第一天(nb。我删除了'y',因为这与我们的问题无关):

BOOL dayFirst = [[NSDateFormatter dateFormatFromTemplate:@"MMMMd" options:0 locale:[NSLocale currentLocale]] hasPrefix:@"d"];

NB. The docs for dateFormatFromTemplate state that "The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied." given this, sometimes the test may return FALSE due to an unknown format (meaning it defaults to month-first when unclear). Decide for yourself which default you prefer, or if you need to enhance the test to support additional locales.

NB。 dateFormatFromTemplate的文档声明“返回的字符串可能不包含模板中给出的那些组件,但可能 - 例如 - 应用了特定于语言环境的调整。”鉴于此,有时由于格式未知,测试可能会返回FALSE(意味着默认情况下默认为月份优先)。确定您喜欢哪个默认值,或者您是否需要增强测试以支持其他语言环境。