iPhone - 如何获取用户的日期格式

时间:2022-09-18 07:28:53

How can I know what date format user prefers? Is there a way to read it from phone's local settings? Does the user prefer to read date in format "dd/mm/yyyy" or "mm/dd/yyyy"?

我怎么知道用户喜欢的日期格式?有没有办法从手机的本地设置中读取它?用户是否更喜欢以“dd / mm / yyyy”或“mm / dd / yyyy”格式阅读日期?

Thank you in advance.

先感谢您。

7 个解决方案

#1


Use NSDateFormatter, but set the style (not the format) so it uses the user's locale formats.

使用NSDateFormatter,但设置样式(不是格式),以便它使用用户的语言环境格式。

e.g. create an NSDateFormatter and configure it thusly

例如创建一个NSDateFormatter并进行配置

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

(for mm/dd/yy type dates, which are dd/mm/yy in other countries)

(对于mm / dd / yy类型日期,在其他国家/地区为dd / mm / yy)

#2


CFDateFormatter defines several date and time format styles—short, medium, long, and full. It also defines a "none" style that you can use to suppress output of a component. The use of styles is illustrated in “Using Date Format Styles.” The date and time styles do not specify an exact format—they depend on the locale, the user preference settings, and the operating system version. If you want an exact format, use the CFDateFormatterSetFormat function to change the format strings, as shown in “Using Date Format Strings.”

CFDateFormatter定义了几种日期和时间格式样式 - 短,中,长和完整。它还定义了一种“无”样式,可用于抑制组件的输出。 “使用日期格式样式”中说明了样式的使用。日期和时间样式未指定确切的格式 - 它们取决于区域设置,用户首选项设置和操作系统版本。如果需要精确格式,请使用CFDateFormatterSetFormat函数更改格式字符串,如“使用日期格式字符串”中所示。

CFDateFormatter

Should do the trick.

应该做的伎俩。

#3


Do you really want to know the exact format, or just convert between NSDate and NSString objects according to it? If the latter, then NSDateFormatter is your man, automatically producing the right format for the user's preferences.

你真的想知道确切的格式,还是只根据它转换NSDate和NSString对象?如果是后者,那么NSDateFormatter就是你的男人,自动为用户的偏好生成正确的格式。

#4


To know which format user prefer: i used [[dateFormatter locale] localeIdentifier] . If it returns en_US it means date is in mm/dd/yyyy format otherwise dd/mm/yyyy format. dateFormatter is an instance of NSDateFormatter class.

要知道用户喜欢哪种格式:我使用[[dateFormatter locale] localeIdentifier]。如果它返回en_US,则表示日期为mm / dd / yyyy格式,否则为dd / mm / yyyy格式。 dateFormatter是NSDateFormatter类的一个实例。

#5


Just in case you want the locale settings before formatting a date, you need to use NSLocale, like so:

如果您想在格式化日期之前设置区域设置,则需要使用NSLocale,如下所示:

NSLocale *locale = [NSLocale currentLocale];

This contains a slew of information about the current locale settings - read the docs for detailed info.

这包含有关当前区域设置的大量信息 - 阅读文档以获取详细信息。

#6


Vishwa, the US isn't the only country to use the "mm/dd/yyy" format, and "dd/mm/yyyy" isn't the only alternative.

Vishwa,美国不是唯一使用“mm / dd / yyy”格式的国家,“dd / mm / yyyy”不是唯一的选择。

See http://en.wikipedia.org/wiki/Calendar_date#List_of_the_world_locations_by_date_format_in_use for more info.

有关详细信息,请参阅http://en.wikipedia.org/wiki/Calendar_date#List_of_the_world_locations_by_date_format_in_use。

#7


Since iOS 6 you can use "localizedStringFromDate"

从iOS 6开始,您可以使用“localizedStringFromDate”

NSString*localizedDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];

#1


Use NSDateFormatter, but set the style (not the format) so it uses the user's locale formats.

使用NSDateFormatter,但设置样式(不是格式),以便它使用用户的语言环境格式。

e.g. create an NSDateFormatter and configure it thusly

例如创建一个NSDateFormatter并进行配置

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

(for mm/dd/yy type dates, which are dd/mm/yy in other countries)

(对于mm / dd / yy类型日期,在其他国家/地区为dd / mm / yy)

#2


CFDateFormatter defines several date and time format styles—short, medium, long, and full. It also defines a "none" style that you can use to suppress output of a component. The use of styles is illustrated in “Using Date Format Styles.” The date and time styles do not specify an exact format—they depend on the locale, the user preference settings, and the operating system version. If you want an exact format, use the CFDateFormatterSetFormat function to change the format strings, as shown in “Using Date Format Strings.”

CFDateFormatter定义了几种日期和时间格式样式 - 短,中,长和完整。它还定义了一种“无”样式,可用于抑制组件的输出。 “使用日期格式样式”中说明了样式的使用。日期和时间样式未指定确切的格式 - 它们取决于区域设置,用户首选项设置和操作系统版本。如果需要精确格式,请使用CFDateFormatterSetFormat函数更改格式字符串,如“使用日期格式字符串”中所示。

CFDateFormatter

Should do the trick.

应该做的伎俩。

#3


Do you really want to know the exact format, or just convert between NSDate and NSString objects according to it? If the latter, then NSDateFormatter is your man, automatically producing the right format for the user's preferences.

你真的想知道确切的格式,还是只根据它转换NSDate和NSString对象?如果是后者,那么NSDateFormatter就是你的男人,自动为用户的偏好生成正确的格式。

#4


To know which format user prefer: i used [[dateFormatter locale] localeIdentifier] . If it returns en_US it means date is in mm/dd/yyyy format otherwise dd/mm/yyyy format. dateFormatter is an instance of NSDateFormatter class.

要知道用户喜欢哪种格式:我使用[[dateFormatter locale] localeIdentifier]。如果它返回en_US,则表示日期为mm / dd / yyyy格式,否则为dd / mm / yyyy格式。 dateFormatter是NSDateFormatter类的一个实例。

#5


Just in case you want the locale settings before formatting a date, you need to use NSLocale, like so:

如果您想在格式化日期之前设置区域设置,则需要使用NSLocale,如下所示:

NSLocale *locale = [NSLocale currentLocale];

This contains a slew of information about the current locale settings - read the docs for detailed info.

这包含有关当前区域设置的大量信息 - 阅读文档以获取详细信息。

#6


Vishwa, the US isn't the only country to use the "mm/dd/yyy" format, and "dd/mm/yyyy" isn't the only alternative.

Vishwa,美国不是唯一使用“mm / dd / yyy”格式的国家,“dd / mm / yyyy”不是唯一的选择。

See http://en.wikipedia.org/wiki/Calendar_date#List_of_the_world_locations_by_date_format_in_use for more info.

有关详细信息,请参阅http://en.wikipedia.org/wiki/Calendar_date#List_of_the_world_locations_by_date_format_in_use。

#7


Since iOS 6 you can use "localizedStringFromDate"

从iOS 6开始,您可以使用“localizedStringFromDate”

NSString*localizedDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];