2つのNSDateの時間差を計算していますが、常に0.0000を返します。タイムスタンプを計算するのに同じロジックを使用していますが、正しくフォーマットされています。個別にさかのぼり、それが正しく印刷されますと、私は違いを印刷するとき、それは..ここに日付を計算するためのコードだ...2つの日付間の時間間隔は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