NSDateFormatter无法正常工作? [英] NSDateFormatter not working properly?
问题描述
我的代码,
-(NSString *)trimDateStringInRequiredFormat:(NSString *)dateInString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateInString];
[dateFormatter setDateFormat:@"hh:mm aaa"];
NSString *dateDisplay = [dateFormatter stringFromDate:date];
return dateDisplay;
}
当我的日期是2012-06-06 12:00时,上述工作正常:00,我得到了合适的输出,如上午12:00。但是当我发送我的日期像2012-06-06 15:00:00它没有返回我适当的输出,相反,它返回我null输出。当我试图追踪这个函数时,2012-06-06 15:00:00日期 NSDate * date = [dateFormatter dateFromString:dateInString];
此行不正确转换。为什么会这样?我在这个代码中哪里错了???
The above worked properly when my date is 2012-06-06 12:00:00 and I got appropriate output like 12:00 AM. But when I send my date like 2012-06-06 15:00:00 it doesn't return me appropriate output, instead, it returns me null output. When I'm trying to trace this function the 2012-06-06 15:00:00 date on NSDate *date = [dateFormatter dateFromString:dateInString];
this line not properly converted. Why did this happen? where am I wrong in this code???
推荐答案
尝试做:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
用hh你有12h格式,用HH你用24h格式
With hh you have the 12h format, with HH you use 24h format
这篇关于NSDateFormatter无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!