Tvd
Tvd

Reputation: 4601

IOS Calculate the Time Difference from Now

I get a Unix timestamp (Created at time) from server of which I get the NSDate object using :

NSTimeInterval interval = [str doubleValue];    
NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

I need to find the time difference between the above created time and current time and display in hh:mm:ss format. I coded it :-

    NSTimeInterval timeDiff = [agent.chatStartTimeStamp timeIntervalSinceNow];
    // NSDate *now = [NSDate date];
    // timeDiff = [now timeIntervalSinceDate:agent.chatStartTimeStamp];  // RETURNS NEGATIVE

    // Divide the interval by 3600 and keep the quotient and remainder
    div_t h = div(timeDiff, 3600);
    int hours = h.quot;
    // Divide the remainder by 60; the quotient is minutes, the remainder
    // is seconds.
    div_t m = div(h.rem, 60);
    int minutes = m.quot;
    int seconds = m.rem;

    NSString *str = [NSString stringWithFormat:@"%d:%d%d", hours, minutes, seconds];
    cell.timeLabel.text = str;

    NSLog(@"*********** CV CTRL - AGENT CHATSTARTTIME - %@  TIME DIFFERNCE = %f  STR = %@", agent.chatStartTimeStamp, timeDiff, str);

The logs for the above 2 codes -

AGENT CHATSTART TIME - 1403342129.980000  SET TIME - 2014-06-21 09:15:29 +0000

*********** CV CTRL - AGENT CHATSTARTTIME - 2014-06-21 09:15:29 +0000  TIME DIFFERNCE = 130.419857  STR = 0:210

The above code gives me results as - suppose the value is 0:2:10, then this value reduces to 0:1:40, 0:1:6, 0:-1....

What I am looking out is - the time difference should increase as the created at time will be something before/earlier current time only. So I want that value of startTime should be deducted from now i.e. now - startTime (time). And I believe this will give me results as I am expecting. I tried with [now timeIntervalSinceDate:agent.chatStartTimeStamp]; but that returns negative response.

UPDATE

This is how I convert the unix timestamp to loca time :-

+ (NSDate *)getNSDateFromUnixTimeStamp : (NSString *) unixTime {
NSString *str = [NSString stringWithFormat:@"%f",[unixTime doubleValue]/(double)1000];

NSTimeInterval interval = [str doubleValue];

NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

str = nil;
unixTime = nil;

return timeStamp;

}

And log for the same :-

2014-06-23 12:24:38.046 MintChat[1021:70b] AGENT CHATSTART TIME - 1403506610.771000  SET TIME - 2014-06-23 06:56:50 +0000

My system time is 12:24:38 & the unixtimestamp is also the current time at just few secs before, so I guess shouldn't the unix time set should also have time as almost same.

Can anyone help me how to get this simple time difference. Where am I going wrong ? I searched a lot on the subject, but couldn't get the expected results.

Any help is highly appreciated.

Thanks

Upvotes: 0

Views: 2103

Answers (1)

Paulw11
Paulw11

Reputation: 115051

Your commented code, [now timeIntervalSinceDate:agent.chatStartTimeStamp]; is correct. From the NSDate documentation -

Return Value

The interval between the receiver and the current date and time. If the receiver is earlier than the current date and time, the return value is negative.

So, you can simply take the absolute value to get the number of seconds -

NSTimeInterval timeDiff = fabs([now timeIntervalSinceDate:agent.chatStartTimeStamp]);

Once you have the time interval you can use this answer to format it -

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeDiff];    
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"hh:mm:ss %@", formattedDate);

Upvotes: 1

Related Questions