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 显示特定于语言环境的日期?
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
