如何使 NSDateFormatter 显示特定于语言环境的日期? [英] How to make NSDateFormatter display locale-specific date?

查看:20
本文介绍了如何使 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 会根据 setDateStylesetTimeStyle<中指定的内容自动选择要显示的内容/代码>.

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 显示特定于语言环境的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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