Time difference between 2 timespans fails to return expected data
Asked Answered
C

5

6

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?

Coulisse answered 20/7, 2011 at 20:53 Comment(0)
Q
7

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;
Quar answered 20/7, 2011 at 21:6 Comment(1)
Thanks everyone for the info. I'll check on why the times vary by so many days. In theory, they should be the same day, but who knows.Coulisse
H
3

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;
Hijack answered 20/7, 2011 at 20:55 Comment(5)
If i do total minutes, it has me as -4317 int timeDifference = (int)currentTs.Subtract(towTime).TotalMinutes;Coulisse
@Coulisse that is the time difference, if you look at what you are passing in, are you sure you are not looking for DateTime if you want to compare time on different days?Hijack
@Jason: 43995 - 303072 = -259077, -259077/60 = -4317.95Hijack
I guess that is what i am unsure of. I was hoping that it would look at the difference in minutes, not taking in account the days. Like i said in my orig, for those 2 times, i was hoping for a difference of around 2 minutes. Can you show me what i should be doing? I will be out for the rest of the day. WIll reply back early tomorrowCoulisse
@Coulisse See Bodrick's answer, that sounds like what you are looking for.Masuria
T
3

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;
}
Timaru answered 20/7, 2011 at 21:24 Comment(0)
N
1

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.

Noun answered 20/7, 2011 at 21:24 Comment(0)
G
0

currentTime is less then towTime (you seem to be missing one digit).

Gulgee answered 20/7, 2011 at 20:55 Comment(1)
The values i am passing in are correct. That is exactly what my program is looking at.Coulisse

© 2022 - 2024 — McMap. All rights reserved.