C# 时间跨度毫秒与总毫秒
在下面的示例中,为什么 Milliseconds
属性返回 0
而 TotalMilliseconds
属性返回 5000
?
// 5 seconds
TimeSpan intervalTimespan = new TimeSpan(0, 0, 5);
// returns 0
intervalTimespan.Milliseconds;
// returns 5000.0
intervalTimespan.TotalMilliseconds
In the example below, why does the Milliseconds
property return 0
but the TotalMilliseconds
property return 5000
?
// 5 seconds
TimeSpan intervalTimespan = new TimeSpan(0, 0, 5);
// returns 0
intervalTimespan.Milliseconds;
// returns 5000.0
intervalTimespan.TotalMilliseconds
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简单:
毫秒
是剩余的毫秒,不构成一整秒。TotalMilliseconds
是以毫秒表示的时间跨度的完整持续时间。Simple:
Milliseconds
are the remaining milliseconds, that don't form a whole second.TotalMilliseconds
is the complete duration of the timespan expressed as milliseconds.因为
Milliseconds
返回毫秒部分,而 TotalMilliseconds 返回由Timespan
表示的总毫秒数示例:0:00:05.047
毫秒:47
总毫秒数:5047
Because
Milliseconds
returns the Milliseconds portion, and TotalMilliseconds returns the total milliseconds represented by theTimespan
Example: 0:00:05.047
Milliseconds: 47
Total Milliseconds: 5047
发生这种情况是因为
intervalTimespan.Milliseconds
返回时间跨度的毫秒部分。在时间跨度构造函数中,只有小时、分钟和秒部分,这就是结果为 0 的原因。
intervalTimespan.TotalMilliseconds
获取时间跨度的总毫秒数。例子:
This hapens because
intervalTimespan.Milliseconds
returns the millisecond component of the timespan.In your timespan constructor, you only have hour, minute, and second components, which is why the result is 0.
intervalTimespan.TotalMilliseconds
gets you the total milliseconds of the timespan.Example:
TimeSpan
还有其他重载:Milliseconds
属性返回实际的毫秒值。TotalMilliseconds
属性返回总毫秒数,包括天、小时、分钟和秒。TimeSpan
has other overloads:The
Milliseconds
property returns the actual milliseconds value.The
TotalMilliseconds
property returns the overall milliseconds including days, hours, minutes, and seconds.Miliseconds
仅返回TimeSpan
的毫秒部分,而TotalMilliseconds
计算TimeSpan
表示的时间中有多少毫秒。在您的情况下,第一个返回
0
因为您正好有 5 秒,第二个返回5000
因为 5s == 5000msMiliseconds
returns just the milliseconds part of yourTimeSpan
, whileTotalMilliseconds
calculates how many milliseconds are in time represented byTimeSpan
.In your case, first returns
0
because you have exactly 5 seconds, second returns5000
because 5s == 5000ms其他事情没有提到的一件重要的事情是(根据文档):
这也可以从
TotalMilliseconds
的注释中扣除:IMO,这具有巨大的含义,因为如果您想要以秒或毫秒为单位的最精确表示,则必须使用
TotalSeconds
或TotalMilliseconds
属性,它们都是double
类型。One important thing other things don't mention, is that (according to the docs):
That is also deductible from the remarks of
TotalMilliseconds
:This has a huge implication, IMO, because if you want the most precise representation in seconds or milliseconds, you must use
TotalSeconds
orTotalMilliseconds
properties, both of them are of typedouble
.