0
NSTimeInterval executionTime = [self.EndDownloadTime timeIntervalSinceDate:self.StartDownloadTime];
NSLog(@"Execution Time: %f", executionTime);

Result is always "nan". When i switch EndDownloadTime and StartDownloadTime, time is "0.00000"

StartDownloadTime and EndDownloadTime are types of NSDate.

How do i get the seconds between the two dates?

Thank you in advance.

Velocity
  • 169
  • 2
  • 8
  • 1
    What you are doing looks correct to me. Log the start and end time values and add them to your question. – trojanfoe Jun 07 '14 at 14:17
  • When you are debugging a problem and everything looks correct but the code is failing then an assumption must be wrong. There are two assumptions here (at least): the values of self.EndDownloadTime and self.StartDownloadTime. NSLog them or use the debugger examining the values as you step through. Note: In Cocoa* properties and variables and method names by convention begin with a lower case letter, case names with an uppercase letter. – zaph Jun 07 '14 at 14:38

2 Answers2

0

Your code looks right. Try this and print value of variables to know if the two dates are correct.


NSTimeInterval executionTime = [self.EndDownloadTime timeIntervalSince1970] - [self.StartDownloadTime timeIntervalSince1970];
NSLog(@"Execution Time: %f", executionTime);

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
0

The simplest solution is a better method, check the docs when things seem complicated:

NSTimeInterval executionTime = [self.EndDownloadTime timeIntervalSinceDate:self.StartDownloadTime];

When you are debugging a problem and everything looks correct but the code is failing then an assumption must be wrong. There are two assumptions here (at least): the values of self.EndDownloadTime and self.StartDownloadTime. NSLog them or use the debugger examining the values as you step through.

Check everything:

NSLog(@"StartDownloadTime: %@", self.StartDownloadTime);
NSLog(@"StartloadTimeInterval: %f", [self.StartDownloadTime timeIntervalSince1970]);
NSLog(@"EndDownloadTime: %@", self.EndDownloadTime);
NSLog(@"EndDownloadTimeInterval: %f", [self.EndDownloadTime timeIntervalSince1970]);

NSTimeInterval executionTime = [self.EndDownloadTime timeIntervalSince1970] - [self.StartDownloadTime timeIntervalSince1970];
NSLog(@"Execution Time: %f", executionTime);

Note: In Cocoa* properties and variables and method names by convention begin with a lower case letter, class names with an uppercase letter. This makes it easier for others to understand your code.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
zaph
  • 111,848
  • 21
  • 189
  • 228
  • `StartDownloadTime: (null) StartloadTimeInterval: 0.000000 EndDownloadTime: (null) EndDownloadTimeInterval: 0.000000 Execution Time: 0.000000` The problem is the "null", correct? But when i set the date to a label, it works correctly – Velocity Jun 07 '14 at 14:55
  • Now i know the problem, but i don't know how to solve it. In a first method, startDownloadTime is called an it returns the right value. But when a call startDownloadTime in another method, it returns (null). But i didn't change the variable.. :/ – Velocity Jun 07 '14 at 15:08
  • Please do two things: 1. Create a new question with the new problem. 2. Accept answers that provided the answer or are most helpful. This will increase your reputation and capabilities on SO. – zaph Jun 07 '14 at 15:17
  • 1
    @Velocity - before creating a new question, try to figure it out. Search code for touches to the two NSDates, implement a custom setter - (void)setStartDownloadTime:(NSDate *)date and add a breakpoint, etc. It seems this was a premature question, please don't create another. – danh Jun 07 '14 at 15:19