简体   繁体   中英

iPhone SDK - NSDateFormatter day + month

I would like to display only the day and month of a date. Basically the NSDateFormatterLongStyle but without the year.

The problem I'm facing is the localization. In the english version it has to show for example 'January 14' and in french '14 Janvier'.

Is there an easy way to print such dates? Or do I have use ifelse statements for checking the current locale and set the day before or after the month?

Thanks in advance for your help.

看看这个问题.....我以前遇到过同样的问题但是通过使用dateFormat而不是dateStyle来解决它

This function looks like it will get you most of the way there if not all...

+ (NSString *)dateFormatFromTemplate:(NSString *)templateoptions:(NSUInteger)optslocale:(NSLocale *)locale

http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options:locale:

"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)."

I think if you use their example provided, just remove the y from

NSString *dateComponents = @"yMMMMd";

and it will give you a string without the year portion for the given locale

It seems like an if/else wouldn't be sufficient to the task of catching every case. I would use the NSDateFormatterLong style, then just find/replace to remove the year from the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM