此方法用于获取表示指定小时数的TimeSpan,精确到最接近的毫秒。
Syntax: public static TimeSpan FromHours (double value);
Parameter:
value: This parameter specifies the number of hours, accurate to the nearest millisecond.
Return Value: It returns a new TimeSpan object that represents the value.
例外情况:
- OverflowException :当给定的double值小于最小可能值或大于最大可能值,或者该值是PositiveInfinity或NegativeInfinity时,将发生OverflowException。
- ArgumentException:如果该值等于NaN。
下面的程序说明了TimeSpan.FromHours(Double)方法的用法:
程序1:
// C# program to demonstrate the
// TimeSpan.FromDays(Double) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
TimeSpan interval = TimeSpan.FromHours(12.3459);
Console.WriteLine("The Timespan is : {0}",
interval);
}
catch (OverflowException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
The Timespan is : 12:20:45.2400000
程序2:对于溢出异常
// C# program to demonstrate the
// TimeSpan.FromDays(Double) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
TimeSpan interval =
TimeSpan.FromHours(Double.NegativeInfinity);
Console.WriteLine("The Timespan is : {0}",
interval);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Exception Thrown: System.OverflowException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.timespan.fromhours?view=netframework-4.7.2