以编程方式从我的iPhone获取日期格式

时间:2014-03-28 13:19:08

标签: ios datetime nsdateformatter

我想在我的iphone应用中使用 DateFormatter 。用于设置 - >常规 - >日期和时间。

先谢谢。

1 个答案:

答案 0 :(得分:1)

使用默认本地(这是用户在设置 - >常规 - >日期&时间中选择的内容)Apple提供了一种快速方法localizedStringFromDate:dateStyle:timeStyle:

仅举例说明使用日期:

NSString *datePresentaion = 
[NSDateFormatter localizedStringFromDate:someDate dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];

如果您要创建NSDateFormatter的实例,则需要告诉用户本地的实例:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale currentLocale];