开发者

C# count of timespan within a timespan

I would like to know a way to do this in C#

Let's say I have 2 timespans : TS1 is 3h and TS2 is 12h.

What is the fastest way to cal开发者_如何学Pythonculate how many times TS1 can go within TS2? In that case, the output would be 4.

if TS1 is 8 days and TS2 is 32 days, it would return 4 as well.


Yes, use integer division. But the devil is in the details, be sure to use an integral property of a TimeSpan to avoid overflow and round-off problems:

 int periods = (int)(TS1.Ticks / TS2.Ticks);


Integer division?

(int) TS1.TotalMilliseconds/(int) TS2.TotalMilliseconds;


You can divide the TotalMilliseconds from one by the other. That is:

double times = TS2.TotalMilliseconds / TS1.TotalMilliseconds


int count = (int)(ts2.TotalSeconds / ts1.TotalSeconds);

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜