1
我想格式化从RSS提要拉的博客文章的日期以显示在表单元格中。我将字符串“dateString”设置为等于从XML解析器接收到的日期并记录结果。然后,我将日期转换为新格式,将其记录到控制台,然后收到“空”。这反过来给我一个0的时间间隔,并在我的手机显示“秒前”。我认为我的日期格式可能存在错误,但我查了很多来源。错误格式化时间objective-c
NSString *dateString = nil;
dateString = [datearray objectAtIndex:indexPath.row];
NSLog(@"Date Posted: %@", dateString);//Log date posted
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(@"New Date: %@", date);//Log date posted
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
NSLog(@"Time interval %f", timeInterval);
NSString *newTime = [timeIntervalFormatter stringForTimeInterval:timeInterval];
NSLog(@"New Time %@", newTime);
cell.dateLabel.text = [NSString stringWithFormat:@"%@", newTime];
控制台登录:
2014-06-13 15:43:41.555 Cazenovia High School[13489:60b] Date Posted: Thu, 22 Aug 2013 13:33:44 +0000
2014-06-13 15:43:41.556 Cazenovia High School[13489:60b] New Date: (null)
2014-06-13 15:43:41.557 Cazenovia High School[13489:60b] Time interval 0.000000
2014-06-13 15:43:41.558 Cazenovia High School[13489:60b] New Time seconds ago
编辑: 添加以下行,但仍然没有改变...
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormat setLocale:locale];
尝试设置的日期格式的区域设置en_US_POSIX'的'特殊语言环境。 – rmaddy
@rmaddy我上面做了编辑,仍然没有改变。 – SuperAdmin
在我身边,这是控制台日志在复制和粘贴代码后所保存的内容:'New Date:2013-08-22 13:33:44 + 0000'和'Time interval -25591284.694682'。这对我来说是正确的。什么是实际的'locale'? – holex