我已经尝试了一切,但无法使其正常工作。如果我将变量“dateStr”设置为@“20082233”,一切都很完美。
当我转换为日期时,它返回一个空值。即使我将dateStr设置为没有stringWithFormat的NSSTring,它仍然不起作用。日期结果返回NULL。
我做错了什么?
// If I manually set it, it works great
// NSString *dateStr = @"20081122";
// If I pull the value from the array, it does not work even if the value is exactly the same.
NSString *dateStr = [NSString stringWithFormat:@"%@", [array1 objectForKey:@"date_string"]];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(@"Date Conversion1: %@", date);
NSLog(@"Date Conversion: %@", dateStr);
NSLog(@"Original value: %@",[array1 objectForKey:@"date_string"] );
values:
2012-04-28 17:08:04.226 ModaApp2[4286:13503] Date Conversion1: (null)
2012-04-28 17:08:04.227 ModaApp2[4286:13503] Date Conversion: (null)
2012-04-28 17:08:04.228 ModaApp2[4286:13503] Original value: 20081122