Find difference between two timestamps - iOS

11,370

Solution 1

I recognize that timestamp! If you're going to get a timestamp as a string, and then convert it back to a double, you can just get it as a double.

Fix:

NSString *timestamp = [NSString stringWithFormat:@"%f", [[NSDate new] timeIntervalSince1970]];
double current = [timestamp doubleValue];
NSTimeInterval difference = [[NSDate dateWithTimeIntervalSince1970:current] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:1296748524]];

NSLog(@"difference: %f", difference);

Better:

double currentt = [[NSDate new] timeIntervalSince1970];
NSTimeInterval differ= [[NSDate dateWithTimeIntervalSince1970:currentt] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:1296748524]];
NSLog(@"differ: %f", differ);

But what you're really doing is converting a date to a timestamp to a string to a timestamp to a date to a timestamp, so why not just use that from the beginning and use:

Best:

double timeStampFromJSON = 1296748524; // or whatever from your JSON
double dif = [[NSDate date] timeIntervalSince1970] - timeStampFromJSON;
NSLog(@"dif: %f", dif);

All will be same result.

Solution 2

timeIntervalSinceDate: expects to receive an NSDate. Also, it is an instance method for an NSDate. Yes, the return value is, essentially, a double, but it's a function using NSDate objects.

Here's an example of appropriate usage:

NSDate *date1 = [NSDate ... // some way to get a valid NSDate
NSDate *date2 = [NSDate ... // some way to get a valid NSDate

NSTimeInterval elapsed = [date1 timeIntervalSinceDate:date2];

In the above example, "elapsed" will contain the number of seconds that have elapsed between "date1" and "date2". If "date1" is prior to "date2", the value will be negative.

Share:
11,370
Supertecnoboff
Author by

Supertecnoboff

I’m Daniel Sadjadian - Car enthusiast, computer programmer & entrepreneur running my own business. I live life to the max and try my best to get the most out of each day. Every moment counts :)

Updated on June 17, 2022

Comments

  • Supertecnoboff
    Supertecnoboff almost 2 years

    I am trying to use NSTimeInterval to figure out the difference between two timestamps in my iOS application. However I when trying to pass in my timestamp, I get the following error:

    Bad receiver type 'double'

    Here is my code:

    // Get the current date/time in timestamp format.
    NSString *timestamp = [NSString stringWithFormat:@"%f", [[NSDate new] timeIntervalSince1970]];
    double current = [timestamp doubleValue];
    
    // Find difference between current timestamp and
    // the timestamp returned in the JSON file.
    NSTimeInterval difference = [current timeIntervalSinceDate:1296748524];
    

    I thought that NSTimeInterval is just a another meaning for double.. is it not?

    Note that '1296748524' is just being used here as a test.

    I don't understand what I am doing wrong.

    Thanks for you're time :)

  • mbm29414
    mbm29414 about 10 years
    I disagree that this is "best". First of all "[NSDate new]", which functional, is not the dominant idiom; "[NSDate date]" or "[[NSDate alloc] init]" are. Second, I believe your "best" code would be extremely difficult to figure out 6 months later.
  • Logan
    Logan about 10 years
    I agree with you on the date, and I updated, but I can't imagine it ever being difficult to figure out. You are just getting the interval of the current date. Every other function does the same thing but adds unnecessary steps. It's a pretty basic line of code, and every other method has blatant redundancies.
  • Logan
    Logan about 10 years
    also [[NSDate alloc]init] is pretty much the same as [NSDate new]
  • mbm29414
    mbm29414 about 10 years
    Well, are you actually hard-coding that value? You said you recognized it, so maybe it's a reference date, but I don't recognize it. Without comments or more standard Objective-C, this code isn't friendly to another developer. Also, "new" is implemented on NSObject to call alloc] init] (meaning it's exactly the same), but it still isn't standard coding practice. I'm not saying the code isn't functional, but if I were working on code and saw this, it would be a frustrating discovery.
  • Logan
    Logan about 10 years
    I'm not hardcoding it, it's the value provided in the question. I'm assuming it has some significance to them, and I assume that if it's something they need to remember, they'll assign it a variable.
  • mbm29414
    mbm29414 about 10 years
    And I agree that your "best" is now best!
  • Logan
    Logan about 10 years
    All is well in the kingdom!
  • mbm29414
    mbm29414 about 10 years
    "All bow before the mighty Logan!" ;-)
  • Supertecnoboff
    Supertecnoboff about 10 years
    @Logan But how do I display this difference in days? The difference timestamp when converted using a simple epoch converter just changes into a date... How do I change it into days? I want the difference in days.
  • Logan
    Logan about 10 years
    The difference is in seconds, a day is 60 * 60 * 24 seconds (seconds, minutes, hours) Take your difference and divide by the day value, that's how many days difference it is.
  • Supertecnoboff
    Supertecnoboff about 10 years
    @Logan Thank you very much sir :)