iOS:如果区域设置合适,以24小时格式显示时间?

时间:2011-11-22 23:18:31

标签: ios time locale

我的iOS应用将显示当前时间。如何确定用户的区域设置是否更喜欢12小时或24小时格式?

2 个答案:

答案 0 :(得分:1)

请参阅NSDateFormatter类http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

类似的东西:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];

NSString *dateString = [dateFormatter stringFromDate:date];

答案 1 :(得分:0)

在向用户显示数据时,通常无需弄清楚用户的区域设置。如果你使用相关的NSFormatterNSNumberFormatter代表数字,NSDateFormatter代表日期和时间(在这种情况下是相关的),那么你将自动获得输出适合用户的语言环境...即Apple称之为“localized representation of the object