NSDateFormatter:根据currentLocale的日期,没有Year [英] NSDateFormatter: Date according to currentLocale, without Year

查看:102
本文介绍了NSDateFormatter:根据currentLocale的日期,没有Year的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不会太难..

我想显示没有年份的日期。例如:Aug,2nd(美国)或02.08。 (德国)
它也必须适用于其他一些语言环境。

I want to display a date without the year. For example: "Aug, 2nd" (USA) or "02.08." (GERMANY) It must work for a bunch of other locales as well.

到目前为止,我唯一的想法是用年份做一个正常的格式,然后从生成的字符串中删除年份部分。

My only idea so far is to do a normal format with year, and then remove the year-portion from the generated string.

推荐答案

我认为你需要看看:

+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale

根据文档:


返回一个本地化的日期格式字符串,表示为指定的语言环境适当排列的给定日期格式组件。
返回值
一个本地化的日期格式字符串,表示模板中给出的日期格式组件,为locale指定的语言环境进行了适当的排列。

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. Return Value A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.

返回字符串可能不完全包含模板中给出的那些组件,但可能 - 例如 - 应用了特定于语言环境的调整。

The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.

讨论

不同的语言环境对日期组件的排序有不同的约定。您可以使用此方法为指定区域设置的给定组件集获取适当的格式字符串(通常使用当前区域设置 - 请参阅currentLocale)。

Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).

以下示例显示英国和美国英语的日期格式之间的差异:

The following example shows the difference between the date formats for British and American English:



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

额外代码(将此添加到上面的代码中):

Extra code (add this to the code above):

// 
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.locale = gbLocale;
formatter.dateFormat = dateFormat;
NSLog(@"date: %@", [formatter stringFromDate: [NSDate date]]);

请看这里:
NSDateFormatter类参考

这篇关于NSDateFormatter:根据currentLocale的日期,没有Year的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆