Condividi tramite


TimeSpan.FromHours Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function FromHours ( _
    value As Double _
) As TimeSpan
public static TimeSpan FromHours(
    double value
)

Parameters

  • value
    Type: System.Double
    A number of hours, accurate to the nearest millisecond.

Return Value

Type: System.TimeSpan
An object that represents value.

Exceptions

Exception Condition
OverflowException

value is less than MinValue or greater than MaxValue.

-or-

value is Double.PositiveInfinity.

-or-

value is Double.NegativeInfinity.

ArgumentException

value is equal to Double.NaN.

Remarks

The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to initialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond. Note that, because of the loss of precision of the Double data type, this conversion can generate an OverflowException for values that are near to but still in the range of either MinValue or MaxValue. For example, this causes an OverflowException in the following attempt to instantiate a TimeSpan object.

' The following throws an OverflowException at runtime
Dim maxSpan As TimeSpan = TimeSpan.FromHours(TimeSpan.MaxValue.TotalHours)
// The following throws an OverflowException at runtime
TimeSpan maxSpan = TimeSpan.FromHours(TimeSpan.MaxValue.TotalHours);

Examples

The following code example creates several TimeSpan objects using the FromHours method.

' Example of the TimeSpan.FromHours( Double ) method.

Module Example

   Sub GenTimeSpanFromHours(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal hours As Double)

      ' Create a TimeSpan object and TimeSpan string from 
      ' a number of hours.
      Dim interval As TimeSpan = _
          TimeSpan.FromHours(hours)
      Dim timeInterval As String = interval.ToString()

      ' Pad the end of the TimeSpan string with spaces if it 
      ' does not contain milliseconds.
      Dim pIndex As Integer = timeInterval.IndexOf(":"c)
      pIndex = timeInterval.IndexOf("."c, pIndex)
      If pIndex < 0 Then timeInterval &= "        "

      outputBlock.Text &= String.Format("{0,21}{1,26}", hours, timeInterval) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= "This example of " & _
          "TimeSpan.FromHours( Double )" & _
          vbCrLf & "generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= "{0,21}{1,18}", _
          "FromHours", "TimeSpan" & vbCrLf
      outputBlock.Text &= "{0,21}{1,18}", _
          "---------", "--------" & vbCrLf

      GenTimeSpanFromHours(outputBlock, 0.0000002)
      GenTimeSpanFromHours(outputBlock, 0.0000003)
      GenTimeSpanFromHours(outputBlock, 0.0012345)
      GenTimeSpanFromHours(outputBlock, 12.3456789)
      GenTimeSpanFromHours(outputBlock, 123456.7898765)
      GenTimeSpanFromHours(outputBlock, 0.0002777)
      GenTimeSpanFromHours(outputBlock, 0.0166666)
      GenTimeSpanFromHours(outputBlock, 1)
      GenTimeSpanFromHours(outputBlock, 24)
      GenTimeSpanFromHours(outputBlock, 500.3389445)
   End Sub
End Module

' This example of TimeSpan.FromHours( Double )
' generates the following output.
' 
'             FromHours          TimeSpan
'             ---------          --------
'                 2E-07          00:00:00.0010000
'                 3E-07          00:00:00.0010000
'             0.0012345          00:00:04.4440000
'            12.3456789          12:20:44.4440000
'        123456.7898765     5144.00:47:23.5550000
'             0.0002777          00:00:01
'             0.0166666          00:01:00
'                     1          01:00:00
'                    24        1.00:00:00
'           500.3389445       20.20:20:20.2000000
// Example of the TimeSpan.FromHours( double ) method.
using System;

class Example
{
   static void GenTimeSpanFromHours(System.Windows.Controls.TextBlock outputBlock, double hours)
   {
      // Create a TimeSpan object and TimeSpan string from 
      // a number of hours.
      TimeSpan interval = TimeSpan.FromHours(hours);
      string timeInterval = interval.ToString();

      // Pad the end of the TimeSpan string with spaces if it 
      // does not contain milliseconds.
      int pIndex = timeInterval.IndexOf(':');
      pIndex = timeInterval.IndexOf('.', pIndex);
      if (pIndex < 0) timeInterval += "        ";

      outputBlock.Text += String.Format("{0,21}{1,26}", hours, timeInterval) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
          "This example of TimeSpan.FromHours( double )\n" +
          "generates the following output.\n" + "\n";
      outputBlock.Text += "{0,21}{1,18}",
          "FromHours", "TimeSpan" + "\n";
      outputBlock.Text += "{0,21}{1,18}",
          "---------", "--------" + "\n";

      GenTimeSpanFromHours(outputBlock, 0.0000002);
      GenTimeSpanFromHours(outputBlock, 0.0000003);
      GenTimeSpanFromHours(outputBlock, 0.0012345);
      GenTimeSpanFromHours(outputBlock, 12.3456789);
      GenTimeSpanFromHours(outputBlock, 123456.7898765);
      GenTimeSpanFromHours(outputBlock, 0.0002777);
      GenTimeSpanFromHours(outputBlock, 0.0166666);
      GenTimeSpanFromHours(outputBlock, 1);
      GenTimeSpanFromHours(outputBlock, 24);
      GenTimeSpanFromHours(outputBlock, 500.3389445);
   }
}

/*
This example of TimeSpan.FromHours( double )
generates the following output.

            FromHours          TimeSpan
            ---------          --------
                2E-07          00:00:00.0010000
                3E-07          00:00:00.0010000
            0.0012345          00:00:04.4440000
           12.3456789          12:20:44.4440000
       123456.7898765     5144.00:47:23.5550000
            0.0002777          00:00:01
            0.0166666          00:01:00
                    1          01:00:00
                   24        1.00:00:00
          500.3389445       20.20:20:20.2000000
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.