此方法用于获取表示指定毫秒数的TimeSpan。
用法: public static TimeSpan FromMilliseconds (double value);
参数:
value:此参数指定毫秒数。
返回值:它返回一个代表该值的新TimeSpan对象。
异常:
- OverflowException:当给定的double值小于最小可能值或大于最大可能值,或者该值是PositiveInfinity或NegativeInfinity时,就会发生这种情况。
- ArgumentException:如果该值等于NaN。
以下示例程序旨在说明TimeSpan.FromMilliseconds(Double)方法的用法:
示例1:
// C# program to demonstrate the 
// TimeSpan.FromMilliseconds(Double) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            TimeSpan interval =  
               TimeSpan.FromMilliseconds(8233567398261.2); 
  
            Console.WriteLine("The Timespan is : {0}", 
                                             interval); 
        } 
  
        catch (OverflowException e)  
        { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
输出:
The Timespan is : 95295.22:03:18.2610000
示例2:对于溢出异常
// C# program to demonstrate the 
// TimeSpan.FromMilliseconds(Double) 
// Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            TimeSpan interval =  
              TimeSpan.FromMilliseconds(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/en-us/dotnet/api/system.timespan.frommilliseconds?view=netframework-4.7.2
相关用法
- C# Dictionary.Add()用法及代码示例
- C# Queue.Contains()用法及代码示例
- C# Math.Abs()函数用法及代码示例
- C# Stack.Contains()用法及代码示例
- C# Math.Exp()用法及代码示例
- C# Math.Abs()方法用法及代码示例
注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 TimeSpan.FromMilliseconds() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
