TimeSpan.FromMilliseconds() Method in C#

This method is used to get a TimeSpan that represents a specified number of milliseconds.

Syntax: public static TimeSpan FromMilliseconds (double value);

Paramater:
value: This parameter specifies the number of milliseconds.

Return Value: It returns a new TimeSpan object that represents the value.

Exceptions:

  • OverflowException: It occurs when the given double value is smaller than the smallest possible value or greater than the largest possible value or the value is PositiveInfinity or is NegativeInfinity.
  • ArgumentException: If the value is equal to NaN.

Below programs illustrate the use of TimeSpan.FromMilliseconds(Double) Method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The Timespan is : 95295.22:03:18.2610000

Program 2: For Overflow Exception

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.OverflowException

Reference:



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.