ios – NSDateFormatter:根据currentLocale的日期,不含Year

ios – NSDateFormatter:根据currentLocale的日期,不含Year,第1张

概述这不可能太难了 我想显示一个没有一年的日期.例如:“8月2日”(美国)或“02.08”. (德国) 它也适用于一大堆其他地区. 到目前为止我唯一的想法是用一年的正常格式,然后从生成的字符串中删除年份. 我想你需要看看: + (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale: 这不可能太难了

我想显示一个没有一年的日期.例如:“8月2日”(美国)或“02.08”. (德国)
它也适用于一大堆其他地区.

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

解决方法 我想你需要看看:
+ (Nsstring *)dateFormatFromTemplate:(Nsstring *)template options:(NSUInteger)opts locale:(NSLocale *)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.

discussion

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 exampleNsstring *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 Class Reference

总结

以上是内存溢出为你收集整理的ios – NSDateFormatter:根据currentLocale的日期,不含Year全部内容,希望文章能够帮你解决ios – NSDateFormatter:根据currentLocale的日期,不含Year所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1111668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存