-[NSDateFormatter dateFromString:]返回nil [英] -[NSDateFormatter dateFromString:] returns nil
本文介绍了-[NSDateFormatter dateFromString:]返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
NSLog(@"%@",lower);
NSLog(@"%@",higher);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dtLower = [df dateFromString:lower];
NSDate *dtHigher = [df dateFromString:higher];
[df release];
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"yyyy-MM-dd"];
NSString * lowerDate = [df1 stringFromDate:dtLower];
NSString * higherDate = [df1 stringFromDate:dtHigher];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
[dateLabel setTextColor:[UIColor grayColor]];
dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
[self.view addSubview:dateLabel];
控制台中lower
和higher
的输出是:
2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56
在这里,以上代码对我不起作用. higher
和lower
的值不为null,但是当我尝试使用NSDateFormatter将其转换为NSDate时,它将返回nil.
Here the above code does not work for me. The value for higher
and lower
are not null but when I try converting it into NSDate using NSDateFormatter then it returns nil.
所以dtLower
和dtHigher
返回nil
有什么问题吗?
推荐答案
尝试设置[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
这篇关于-[NSDateFormatter dateFromString:]返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文