Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time difference between 2 timespans fails to return expected data

Tags:

c#

timespan

I have created 2 Timespans below:

TimeSpan currentTs = TimeSpan.FromSeconds(43995); //12:13:15
TimeSpan towTime = TimeSpan.FromSeconds(303072); //12:11:12

I'm trying to find the difference in minutes (by the seconds I'm passing it, it looks like they are on different days). I am hoping for around 2 minutes difference, but in reality, I'm getting -57 minutes.

int timeDifference = (int)currentTs.Subtract(towTime).Minutes;

Can someone explain what i am doing wrong?

like image 408
Jason Avatar asked Jul 20 '11 20:07

Jason


4 Answers

If you are just looking for the difference between the minutes (not including the days, hours or seconds), then you can use

double timeDifference = currentTs.Minutes - towTime.Minutes; // 2

This is just the difference of the minutes component though. As other people have said, these times are separated by 3 days, so a TimeSpan possibly isn't ideal.

Or, If you want the seconds to be included as well, you can use

TimeSpan minutes1 = new TimeSpan (0, currentTs.Minutes, currentTs.Seconds);
TimeSpan minutes2 = new TimeSpan (0, towTime.Minutes, towTime.Seconds);

double timeDifference = minutes1.TotalMinutes - minutes2.TotalMinutes // 2.05;
like image 95
Bodrick Avatar answered Nov 03 '22 02:11

Bodrick


You are looking for the TotalMinutes property not Minutes - the later is just the minutes part of the time difference, the former is the full time difference expressed in fractional minutes:

double timeDifference = currentTs.Subtract(towTime).TotalMinutes;

Also easier to read is:

double timeDifference = (currentTs - towTime).TotalMinutes;
like image 20
BrokenGlass Avatar answered Nov 03 '22 02:11

BrokenGlass


I'm guessing you want to completely ignore the fact that the times are different by about 3 days. In that case, if you take the remainder after division by 86400 (the number of seconds in a day) and pass that to TimeSpan.FromSeconds, you should hopefully get what you want:

TimeSpan currentTs = TimeSpan.FromSeconds(43995 % 86400); //12:13:15
TimeSpan towTime = TimeSpan.FromSeconds(303072 % 86400); //12:11:12, ignoring the 3 days.
double timeDifference = (currentTs - towTime).TotalMinutes;

I got the value of timeDifference as 2.05 when I tried this.

Of course, if currentTs is earlier in the day than towTime (perhaps because midnight falls between them), the time difference will come out negative. If you still want to count the number of minutes between the two, assuming that currentTs is always after towTime, then you'll need to add 1440 (the number of minutes in a day) to timeDifference if it works out negative:

if (timeDifference < 0)
{
    timeDifference += 1440;
}
like image 3
Luke Woodward Avatar answered Nov 03 '22 02:11

Luke Woodward


Because the second timespan isn't 12:11:12, its 3:12:11:12.

So you're going to get a negative result which would be in the thousands if you did total minutes (since its representing something like -2days, 57 minutes, 57 seconds)...since you just ask for Minutes and not TotalMinutes you just get the -57.

like image 1
heisenberg Avatar answered Nov 03 '22 01:11

heisenberg