How to make NSDateFormatter display locale-specific date?(如何使 NSDateFormatter 显示特定于语言环境的日期?)
问题描述
我正在使用 NSDateFormatter 在我的 iPhone 应用程序中设置我的日期,并且日期显示正确.但是,我发现所有语言环境(我的应用程序最多支持 12 种不同的语言)都坚持我通过 setDateFormat 指定的日期格式.理想情况下,我希望日期格式以用户区域设置自然的形式显示,而不是遵循我的格式(这对英语很好,但对于其他语言环境可能不自然).
以下是我的代码:
<上一页>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateStyle:NSDateFormatterMediumStyle];[dateFormatter setTimeStyle:NSDateFormatterShortStyle];[dateFormatter setDateFormat:@"MMMM dd, yyyy kk:mm:ss"];self.creationDate.text = [dateFormatter stringFromDate:creationTimeStamp];找到了解决方案.关键是根本不调用 setDateFormat
,iPhone 中的 API 会根据 setDateStyle
和 setTimeStyle<中指定的内容自动选择要显示的内容/代码>.
I am using NSDateFormatter to set my date in my iPhone app, and dates are showing up properly. However, I am finding all the locales (my app supports up to 12 different languages) are sticking to the date format I specify via setDateFormat. Ideally, I want the date format to appear in a form natural to the region setting for the user, instead of following my format (which works well for English but may not be natural for other locales).
Following is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; [dateFormatter setDateFormat:@"MMMM dd, yyyy kk:mm:ss"]; self.creationDate.text = [dateFormatter stringFromDate:creationTimeStamp];
Found the solution. The key is to not call setDateFormat
at all, and the API in iPhone would automatically pick the appropriate thing to display based on what is specified in setDateStyle
and setTimeStyle
.
这篇关于如何使 NSDateFormatter 显示特定于语言环境的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 NSDateFormatter 显示特定于语言环境的日期?


- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01