2个时间跨度之间的时间差未能返回预期数据
我在下面创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只是寻找分钟之间的差异(不包括天、小时或秒),那么您可以使用
This is just the Difference of the minutes 组件。正如其他人所说,这些时间相隔 3 天,因此 TimeSpan 可能并不理想。
或者,如果您还希望包含秒数,则可以使用
If you are just looking for the difference between the minutes (not including the days, hours or seconds), then you can use
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
您正在寻找
TotalMinutes
属性而不是Minutes
- 后者只是时差的分钟部分,前者是以小数分钟表示的完整时差:也更容易阅读内容是:
You are looking for the
TotalMinutes
property notMinutes
- the later is just the minutes part of the time difference, the former is the full time difference expressed in fractional minutes:Also easier to read is:
我猜您想完全忽略时间相差大约 3 天的事实。在这种情况下,如果您除以 86400(一天中的秒数)后取余数并将其传递给 TimeSpan.FromSeconds,您应该有望得到您想要的
结果:我得到了
timeDifference
当我尝试这个时,为 2.05。当然,如果
currentTs
比towTime
早(可能是因为它们之间是午夜),则时差将为负值。如果您仍然想计算两者之间的分钟数,假设currentTs
始终在towTime
之后,那么您需要添加 1440(如果结果为负,则将一天)转换为timeDifference
: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:
I got the value of
timeDifference
as 2.05 when I tried this.Of course, if
currentTs
is earlier in the day thantowTime
(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 thatcurrentTs
is always aftertowTime
, then you'll need to add 1440 (the number of minutes in a day) totimeDifference
if it works out negative:因为第二个时间跨度不是 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.
currentTime 小于 towTime (您似乎缺少一位数字)。
currentTime is less then towTime (you seem to be missing one digit).