- [NSDateFormatter dateFromString:]返回nil

Par*_*att 2 cocoa-touch objective-c nsdateformatter ios

    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];
Run Code Online (Sandbox Code Playgroud)

控制台的输出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
Run Code Online (Sandbox Code Playgroud)

这里的代码对我不起作用.for higher和的值lower不为null但是当我尝试使用NSDateFormatter将其转换为NSDate时,它返回nil.

所以dtLower,dtHigher返回零

可能有什么不对?

Kin*_*iss 8

尝试设置 [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];