Multiply TimeSpan in .NET

Colonel Panic picture Colonel Panic · Mar 28, 2012 · Viewed 25.7k times · Source

How do I multiply a TimeSpan object in C#? Assuming the variable duration is a TimeSpan, I would like, for example

duration*5

But that gives me an error "operator * cannot be applied to types TimeSpan and int". Here's my current workaround

duration+duration+duration+duration+duration

But this doesn't extend to non-integer multiples, eg. duration * 3.5

Answer

Justin Pihony picture Justin Pihony · Mar 28, 2012

From this article

TimeSpan duration = TimeSpan.FromMinutes(1);
duration = TimeSpan.FromTicks(duration.Ticks * 12);
Console.WriteLine(duration);