开发者

C# Time calculation confusion :: TimeSpan.FromHours(12) will check if its more than 12 hrs

Will TimeSpan.FromHours check if the time is more than 12 hours 0 minutes and 0 seconds?

Or is it ONLY going to check if the number i开发者_StackOverflow社区s more than 12 or not, i.e. ONLY hours? If yes, how do you check if it's not even a second more than 12 hours?


The FromHours method doesn't compare times, it creates a new TimeSpan value. TimeSpan.FromHours(12) creates a TimeSpan with the value 12:00:00.00000.

You can use it to compare it with a TimeSpan value:

if (someTimeSpan > TimeSpan.FromHours(12)) ...

This will not enter the block if someTimeSpan is 12:00:00.00000 but it will if it's 12:00:00.00001 or more.


TimeSpan.FromHours(x) - TimeSpan.FromHours(12) <= TimeSpan.Zero

Or, of course, bypass the TimeSpan entirely before calling FromHours:

if (x - 12 <= 0) return TimeSpan.FromHours(x);


The TimeSpan class represents a span of time, TimeSpan.FromHours(12) is a time span of 12 hours. Maybe you confused it with the DateTime class?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜