C# .NET 中的时间跨度相乘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9909086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiply TimeSpan in .NET
提问by Colonel Panic
How do I multiply a TimeSpan object in C#? Assuming the variable durationis a TimeSpan, I would like, for example
如何在 C# 中乘以 TimeSpan 对象?假设变量duration是一个TimeSpan,我想,例如
duration*5
But that gives me an error "operator * cannot be applied to types TimeSpan and int". Here's my current workaround
但这给了我一个错误“运算符 * 不能应用于 TimeSpan 和 int 类型”。这是我目前的解决方法
duration+duration+duration+duration+duration
But this doesn't extend to non-integer multiples, eg. duration * 3.5
但这不会扩展到非整数倍数,例如。 duration * 3.5
采纳答案by Justin Pihony
TimeSpan duration = TimeSpan.FromMinutes(1);
duration = TimeSpan.FromTicks(duration.Ticks * 12);
Console.WriteLine(duration);
回答by PedroC88
You need to specify which member it is you want to multiply by 5 -> TimeSpan.TotalMinutes * 5
您需要指定要乘以 5 的成员 -> TimeSpan.TotalMinutes * 5
回答by Kent Boogaart
The TimeSpanstructure does not provide an overload for the *operator, so you have to do this yourself:
该TimeSpan结构不为*运算符提供重载,因此您必须自己执行此操作:
var result = TimeSpan.FromTicks(duration.Ticks * 5);
回答by SkonJeet
回答by Kendall Frey
You can use the internal data of TimeSpan, namely ticks.
您可以使用 TimeSpan 的内部数据,即刻度。
TimeSpan day = TimeSpan.FromDays(1);
TimeSpan week = TimeSpan.FromTicks(day.Ticks * 7);
回答by Marcin
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);
回答by Stephen Hewlett
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);
}
twill end up as 150 seconds.
t将结束为 150 秒。
回答by antmeehan
TimeSpan.Multiplyhas arrived in .NET Core, and looks like it will arrive in .NET Standard 2.1:
TimeSpan.Multiply已经出现在 .NET Core 中,看起来它会出现在 .NET Standard 2.1 中:
https://docs.microsoft.com/en-us/dotnet/api/system.timespan.op_multiply?view=netstandard-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);
回答by Deczaloth
Multiply is now available for TimeSpan!!!
乘法现在可用于 TimeSpan !!!
But only for .NET Coreand .NET Standard.
但仅适用于.NET Core和.NET Standard。
Since .NET Core 2.0(or .NET Standard 2.1) you can successfully run the following code:
从.NET Core 2.0(或.NET Standard 2.1)开始,您可以成功运行以下代码:
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+.
然而,重要的是要注意(如文档中所述)这仅适用于.NET Core 2.0+ 和.NET Standard 2.1+。
As of today (26th November 2019) the code above will fail even in the latest .NET Frameworkversion: 4.8.
截至今天(2019 年 11 月 26 日),即使在最新的.NET Framework版本中,上述代码也会失败:4.8。
If you try the code above in a Consoleapplication, for example, running .NET Core 1.1or lower, or .NET Framework 4.8or lower you will be thrown the following exception:
如果您在控制台应用程序中尝试上述代码,例如,运行.NET Core 1.1或更低版本,或.NET Framework 4.8或更低版本,您将抛出以下异常:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
'Operator '*' cannot be applied to operands of type 'System.TimeSpan' and 'int''
Why not in .NET Framework?
为什么不在 .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 Immosays:
为了理解为什么在地球上,我们不能在.NET框架使用上面的代码,它是启发看到什么IMMO说:
.NET Coreis 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.
.NET Core是.NET的开源、跨平台和快速发展的版本。由于它的并行特性,它可以进行我们不能冒险应用回 .NET Framework 的更改。这意味着 .NET Core 将随着时间的推移获得 .NET Framework 无法获得的新 API 和语言功能。在 Build 中,我们展示了文件 API 如何在 .NET Core 上更快的演示。如果我们将这些相同的更改放入 .NET Framework 中,我们可能会破坏现有的应用程序,而我们不想这样做。

