2个时间跨度之间的时间差未能返回预期数据

发布于 2024-11-25 08:22:56 字数 361 浏览 0 评论 0原文

我在下面创建了 2 个时间跨度:

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

我试图找出以分钟为单位的差异(按照我经过的秒数,看起来它们是在不同的日子)。我希望有大约 2 分钟的差异,但实际上,我得到了 -57 分钟。

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

有人可以解释我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丢了幸福的猪 2024-12-02 08:22:56

如果您只是寻找分钟之间的差异(不包括天、小时或秒),那么您可以使用

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

This is just the Difference of the minutes 组件。正如其他人所说,这些时间相隔 3 天,因此 TimeSpan 可能并不理想。

或者,如果您还希望包含秒数,则可以使用

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;

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;
心病无药医 2024-12-02 08:22:56

您正在寻找 TotalMinutes 属性而不是 Minutes - 后者只是时差的分钟部分,前者是以小数分钟表示的完整时差:

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

也更容易阅读内容是:

double timeDifference = (currentTs - towTime).TotalMinutes;

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;
﹎☆浅夏丿初晴 2024-12-02 08:22:56

我猜您想完全忽略时间相差大约 3 天的事实。在这种情况下,如果您除以 86400(一天中的秒数)后取余数并将其传递给 TimeSpan.FromSeconds,您应该有望得到您想要的

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;

结果:我得到了 timeDifference 当我尝试这个时,为 2.05。

当然,如果 currentTstowTime 早(可能是因为它们之间是午夜),则时差将为负值。如果您仍然想计算两者之间的分钟数,假设 currentTs 始终在 towTime 之后,那么您需要添加 1440(如果结果为负,则将一天)转换为 timeDifference

if (timeDifference < 0)
{
    timeDifference += 1440;
}

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;
}
策马西风 2024-12-02 08:22:56

因为第二个时间跨度不是 12:11:12,而是 3:12:11:12。

因此,如果您计算总分钟数,您将得到一个负结果,该结果将是数千(因为它代表类似 -2 天,57 分钟,57 秒)...因为您只要求分钟数而不是 TotalMinutes,您只是得到-57。

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.

这个俗人 2024-12-02 08:22:56

currentTime 小于 towTime (您似乎缺少一位数字)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文