Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to perform division in timespan [duplicate]

Tags:

I have a value in TimeSpan, let's say: tsp1 = 2 hour 5 minutes. I have another TimeSpan variable which contains a value like: tsp2 = 0 hours 2 minutes

Please tell me how I can divide tsp1 by tsp2 so that I can get the exact number of times tsp2 divides into tsp1 and what the remainder is.

I am using Visual Studio 2008.

Thanks.

like image 500
Dr. Rajesh Rolen Avatar asked Nov 29 '10 10:11

Dr. Rajesh Rolen


1 Answers

The simplest approach is probably just to take their lengths in ticks, and divide those. For example:

long ticks1 = tsp1.Ticks; long ticks2 = tsp2.Ticks;  long remainder; long count = Math.DivRem(ticks1, ticks2, out remainder);  TimeSpan remainderSpan = TimeSpan.FromTicks(remainder);  Console.WriteLine("tsp1/tsp2 = {0}, remainder {1}", count, remainderSpan); 
like image 84
Jon Skeet Avatar answered Oct 19 '22 07:10

Jon Skeet