Multiply TimeSpan in .NET

风流意气都作罢 提交于 2019-12-04 08:46:44

问题


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


回答1:


From this article

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



回答2:


For those wishing to copy and paste:

namespace Utility
{
    public static class TimeSpanExtension
    {
        /// <summary>
        /// Multiplies a timespan by an integer value
        /// </summary>
        public static TimeSpan Multiply(this TimeSpan multiplicand, int multiplier)
        {
            return TimeSpan.FromTicks(multiplicand.Ticks * multiplier);
        }

        /// <summary>
        /// Multiplies a timespan by a double value
        /// </summary>
        public static TimeSpan Multiply(this TimeSpan multiplicand, double multiplier)
        {
            return TimeSpan.FromTicks((long)(multiplicand.Ticks * multiplier));
        }
    }
}

Example Usage:

using Utility;

private static void Example()
{
    TimeSpan t = TimeSpan.FromSeconds(30).Multiply(5);
}

t will end up as 150 seconds.




回答3:


The TimeSpan structure does not provide an overload for the * operator, so you have to do this yourself:

var result = TimeSpan.FromTicks(duration.Ticks * 5);



回答4:


You can use the internal data of TimeSpan, namely ticks.

TimeSpan day = TimeSpan.FromDays(1);
TimeSpan week = TimeSpan.FromTicks(day.Ticks * 7);



回答5:


TimeSpan.Multiply has arrived in .NET Core, and looks like it will arrive in .NET Standard 2.1:

https://docs.microsoft.com/en-us/dotnet/api/system.timespan.op_multiply?view=netstandard-2.1

   var result = 3.0 * TimeSpan.FromSeconds(3);



回答6:


Use ticks:

http://www.personal.psu.edu/wbk2/blogs/personal_blog/dotnet-stuff-timespan-multiplication-and-division.html




回答7:


You need to specify which member it is you want to multiply by 5 -> TimeSpan.TotalMinutes * 5




回答8:


The problem here is that you want to multiply timespan. The simplest workaround is to use ticks. eg.

 var ticks = TimeSpan.FromMinutes(1).Ticks;
 var newTimeSpan = TimeSpan.FromTicks(ticks*5);



回答9:


Multiply is now available for TimeSpan!!!

But only for .NET Core and .NET Standard.

Since .NET Core 2.0 (or .NET Standard 2.1) you can successfully run the following code:

Console.WriteLine(TimeSpan.FromSeconds(45) * 3);
// Prints:
// 00:02:15

Limitations

Nevertheless, it is important to note (as described in the docu) that this only applies for .NET Core 2.0+, and .NET Standard 2.1+.

As of today (26th November 2019) the code above will fail even in the latest .NET Framework version: 4.8.

If you try the code above in a Console application, for example, running .NET Core 1.1 or lower, or .NET Framework 4.8 or lower you will be thrown the following exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
'Operator '*' cannot be applied to operands of type 'System.TimeSpan' and 'int''

Why not in .NET Framework?

In order to understand why on earth we can not use the code above in .NET Framework, it is enlightening to see what Immo says:

.NET Core is the open source, cross-platform, and fast-moving version of .NET. Because of its side-by-side nature it can take changes that we can’t risk applying back to .NET Framework. This means that .NET Core will get new APIs and language features over time that .NET Framework cannot. At Build we showed a demo how the file APIs are faster on .NET Core. If we put those same changes into .NET Framework we could break existing applications, and we don’t want to do that.



来源:https://stackoverflow.com/questions/9909086/multiply-timespan-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!