我查看了其他示例,但仍然无法将此字符串转换为日期。运行此代码后,我从 NSLogs 获得以下输出:
预测转换:2012-07-01 00:00:00 +0000
时间戳转换:2012-06-24 00:00:00 +0000
预测:604800.000000
这不是我使用的硬编码日期。有谁知道为什么?代码如下:
NSString *timeStamp = @"20120620 19:23";//[[predictionData objectAtIndex:0 ] valueForKey:@"tmstmp"];
NSString *predictionTime = @"20120620 19:30";// [[predictionData objectAtIndex:0 ] valueForKey:@"prdtm"];
NSDateFormatter *ts = [[NSDateFormatter alloc] init];
[ts setDateFormat:@"yyyyMMdd HH:dd"];
NSDate *convertedTS = [ts dateFromString:timeStamp];
NSDateFormatter *pt = [[NSDateFormatter alloc] init];
[pt setDateFormat:@"yyyyMMdd HH:dd"];
NSDate *convertedPT = [pt dateFromString:predictionTime];
NSTimeInterval timeDifference = [convertedPT timeIntervalSinceDate:convertedTS];
NSLog(@"Prediction Conversion: %@", [convertedPT description]);
NSLog(@"Timestamp Conversion: %@", [convertedTS description]);
NSLog(@"Prediction: %f", timeDifference);
谢谢!