두 NSDate 사이의 시간 차이를 계산하고 항상 0.0000을 반환합니다. 타임 스탬프를 계산하는 데 동일한 논리를 사용하고 있으며 올바르게 형식화했습니다. 별도로 날짜, 올바르게 인쇄됩니다 내가 차이를 인쇄 할 때, 그것은 ... .. 여기에 날짜를 계산하는 코드의 ...두 날짜 사이의 시간 간격은 nil을 반환합니다.
-(NSDate *) toLocalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: [NSDate date]];
return [NSDate dateWithTimeInterval: seconds sinceDate: [NSDate date]];
}
을 0.0000을 인쇄하고 여기에 내가 그 차이를 찾을 코드의
NSLog(@"the time when the location was updated is %@",timeWhenTheLocationWasUpdated);
NSLog(@"the time when the server was contacted last time was %@",timeWhenLastLocationPosted);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss ZZZ"];
NSDate *timeofUpdatingLocation = [[NSDate alloc] init];
NSDate *timeofPostingToTheServer = [[NSDate alloc]init];
timeofUpdatingLocation = [dateFormatter dateFromString:timeWhenTheLocationWasUpdated];
timeofPostingToTheServer = [dateFormatter dateFromString:timeWhenLastLocationPosted];
NSTimeInterval difference = [timeofPostingToTheServer timeIntervalSinceDate:timeofUpdatingLocation];
NSLog(@"the difference between posting the location to the server and updating the location is %f",difference);
NSlog는 코드 시작 부분에 모두 pr입니다. int는 날짜 형식 기에서 언급 한 형식의 날짜를 올바르게 나타냅니다.
실제 로그 출력으로 질문을 업데이트하십시오. 또한'timeofUpdatingLocation'과'timeofPostingToTheServer' (''nil'' 둘 다 추측하고 있습니다)를 기록해야합니다. – rmaddy
그리고'toLocalTime' 메서드는 문제와 어떤 관련이 있습니까? – rmaddy
제 생각 엔'dateFromString'이 두 날짜 모두에서 실패하고 두 날짜를 동일하게 만들고 그 사이에 시간이 0 인 것 같습니다. – Putz1103