Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TimeSpan.FromTicks Method

Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.

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

public static TimeSpan FromTicks (
	long value
)
public static TimeSpan FromTicks (
	long value
)
public static function FromTicks (
	value : long
) : TimeSpan
Not applicable.

Parameters

value

A number of ticks that represent a time.

Return Value

A TimeSpan with a value of value.

This is a convenience method with the same behavior as the System.TimeSpan(Int64) constructor.

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

// Example of the TimeSpan.FromTicks( long ) method.
using System;

class FromTicksDemo
{
    static void GenTimeSpanFromTicks( long ticks )
    {
        // Create a TimeSpan object and TimeSpan string from 
        // a number of ticks.
        TimeSpan    interval = TimeSpan.FromTicks( ticks );
        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 += "        ";

        Console.WriteLine( "{0,21}{1,26}", ticks, timeInterval );
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of TimeSpan.FromTicks( long )\n" +
            "generates the following output.\n" );
        Console.WriteLine( "{0,21}{1,18}", 
            "FromTicks", "TimeSpan" );
        Console.WriteLine( "{0,21}{1,18}", 
            "---------", "--------" );

        GenTimeSpanFromTicks( 1 );
        GenTimeSpanFromTicks( 12345 );
        GenTimeSpanFromTicks( 123456789 );
        GenTimeSpanFromTicks( 1234567898765 );
        GenTimeSpanFromTicks( 12345678987654321 );
        GenTimeSpanFromTicks( 10000000 );
        GenTimeSpanFromTicks( 600000000 );
        GenTimeSpanFromTicks( 36000000000 );
        GenTimeSpanFromTicks( 864000000000 );
        GenTimeSpanFromTicks( 18012202000000 );
    } 
} 

/*
This example of TimeSpan.FromTicks( long )
generates the following output.

            FromTicks          TimeSpan
            ---------          --------
                    1          00:00:00.0000001
                12345          00:00:00.0012345
            123456789          00:00:12.3456789
        1234567898765        1.10:17:36.7898765
    12345678987654321    14288.23:31:38.7654321
             10000000          00:00:01
            600000000          00:01:00
          36000000000          01:00:00
         864000000000        1.00:00:00
       18012202000000       20.20:20:20.2000000
*/

// Example of the TimeSpan.FromTicks( long ) method.
import System.*;

class FromTicksDemo
{
    static void GenTimeSpanFromTicks(long ticks)
    {
        // Create a TimeSpan object and TimeSpan string from 
        // a number of ticks.
        TimeSpan interval = TimeSpan.FromTicks(ticks);
        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 += "        ";
        }
        Console.WriteLine("{0,21}{1,26}", String.valueOf(ticks), timeInterval);
    } //GenTimeSpanFromTicks

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of TimeSpan.FromTicks( long )\n" 
            + "generates the following output.\n"));
        Console.WriteLine("{0,21}{1,18}", "FromTicks", "TimeSpan");
        Console.WriteLine("{0,21}{1,18}", "---------", "--------");
        GenTimeSpanFromTicks(1);
        GenTimeSpanFromTicks(12345);
        GenTimeSpanFromTicks(123456789);
        GenTimeSpanFromTicks(1234567898765L);
        GenTimeSpanFromTicks(12345678987654321L);
        GenTimeSpanFromTicks(10000000);
        GenTimeSpanFromTicks(600000000);
        GenTimeSpanFromTicks(36000000000L);
        GenTimeSpanFromTicks(864000000000L);
        GenTimeSpanFromTicks(18012202000000L);
    } //main
} //FromTicksDemo

/*
This example of TimeSpan.FromTicks( long )
generates the following output.

            FromTicks          TimeSpan
            ---------          --------
                    1          00:00:00.0000001
                12345          00:00:00.0012345
            123456789          00:00:12.3456789
        1234567898765        1.10:17:36.7898765
    12345678987654321    14288.23:31:38.7654321
             10000000          00:00:01
            600000000          00:01:00
          36000000000          01:00:00
         864000000000        1.00:00:00
       18012202000000       20.20:20:20.2000000
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft