-3

NSDateFormatter第一次使用,当我尝试从字符串中获取日期时,它正在给出nil

NSDateFormatter * formatter =  [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];    
[formatter setDateFormat:@"MMM dd , YYYY"];    
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];    
NSDate * date = [formatter dateFromString:@"2013-10-10 09:51:24 +0000"];
NSLog(@"%@",date);

请告诉我在这里缺少什么,在此先感谢。. .

4

2 回答 2

0

试试这种方式...它可能会帮助你

NSDateFormatter * formatter =  [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];

    [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSDate * date = [formatter dateFromString:@"2013-10-10 09:51:24 +0000"];

    NSLog(@"%@",date);  // prints 2013-10-10 09:51:24 +0000

 [formatter setDateFormat:@"MMM dd , YYYY"];

NSString *String = [formatter stringFromDate:date];

NSDate *final = [formatter dateFromString:String];

NSLog(@"%@,%@,%@",date,String,final);
于 2013-10-08T04:55:59.260 回答
0

请尝试以下代码

NSDateFormatter * formatter =  [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

[formatter setTimeZone:[NSTimeZone defaultTimeZone]];

NSDate * date = [formatter dateFromString:@"2013-10-10 09:51:24 +0000"];

[formatter setDateFormat:@"MMM dd , YYYY"];

NSString *dateString = [formatter stringFromDate:date];

NSDate *outDate = [formatter dateFromString:dateString];

NSLog(@"%@,%@,%@",date,dateString,outDate);
于 2013-10-08T05:02:39.863 回答