2015-05-14 59 views
0

我有一点点问题。 请看看这里并理解这个问题。NSDateFormatter在设置日期时没有给出正确的响应

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setLocale:[NSLocale systemLocale]]; 
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"]; 
    self.datePicker.userInteractionEnabled=YES; 
    NSString *dateString=[dateFormatter stringFromDate:datePicker.date]; 

这是我的区块,我正在返回一个日期。但是当我在那里设置NSLocale那么这不会给我适当的回应。

我想DATEdd-MMM-yyyy(24-MAR-2008)甲。但它给响应:

iOS8.0和后者 - >24 M03-2004

iOS7.0和旧版 - >24 Month03-2004

由于一些依赖我不能删除[dateFormatter setLocale:[NSLocale systemLocale]];从我上面的块。 请建议我如何处理这个问题,

+1

[DATEFORMAT setTimeZone:[NSTimeZone localTimeZone];而不是 [dateFormatter setLocale:[NSLocale systemLocale]];你会很好去 –

+0

你能分享你的datepicker.date值吗? – Mukesh

+0

@adnan你的答案是正确的。 –

回答

3

尝试使用

[dateFormatter setLocale:[NSLocale currentLocale]]; 

insteade的

[dateFormatter setLocale:[NSLocale systemLocale]]; 

这将返回你想要的格式。

3

这将解决您的问题

[dateFormatter setLocale:[NSLocale currentLocale]]; 

[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
相关问题