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

查看:98
本文介绍了如何使NSDateFormatter显示特定于语言环境的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSDateFormatter在我的iPhone应用程序中设置日期,并且日期正确显示。但是,我发现所有语言环境(我的应用程序支持多达12种不同的语言)都坚持我通过setDateFormat指定的日期格式。理想情况下,我希望日期格式以自然的形式显示给用户的区域设置,而不是遵循我的格式(适用于英语,但对于其他语言环境可能不太自然)。

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

以下是我的代码:


    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 <中指定的内容自动选择要显示的内容。 / code>和 setTimeStyle

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天全站免登陆