TimeSpan.FromTicks() Method in C#

The TimeSpan.FromTicks() method in C# is used to create a TimeSpan object that represents a specified time duration in ticks. A tick represents one hundred nanoseconds or one ten-millionth of a second, making it the smallest unit of time measurement in .NET.

Syntax

Following is the syntax for the TimeSpan.FromTicks() method −

public static TimeSpan FromTicks(long val);

Parameters

The method accepts the following parameter −

  • val − A long integer representing the number of ticks. Each tick equals 100 nanoseconds.

Return Value

Returns a TimeSpan object that represents the time interval specified by the number of ticks.

Understanding Ticks

TimeSpan Tick Conversions 1 Tick 100 nanoseconds 10,000 Ticks 1 millisecond 10,000,000 Ticks 1 second 600,000,000 Ticks 1 minute 36,000,000,000 Ticks 1 hour TimeSpan.TicksPerSecond = 10,000,000

Using TimeSpan.FromTicks() with Different Tick Values

Example

using System;

public class Demo {
    public static void Main() {
        TimeSpan span1 = TimeSpan.FromTicks(18768768);
        TimeSpan span2 = new TimeSpan(-9, 45, 50);
        TimeSpan span3 = TimeSpan.FromHours(5);
        TimeSpan span4 = TimeSpan.FromMilliseconds(10000);
        TimeSpan span5 = TimeSpan.FromMinutes(2);

        Console.WriteLine("TimeSpan1 = " + span1);
        Console.WriteLine("TimeSpan2 = " + span2);
        Console.WriteLine("TimeSpan3 = " + span3);
        Console.WriteLine("TimeSpan4 = " + span4);
        Console.WriteLine("TimeSpan5 = " + span5);
        Console.WriteLine("Result (Comparison of span1 and span2) = " + TimeSpan.Compare(span1, span2));
        Console.WriteLine("Result (Comparison of span2 and span3) = " + TimeSpan.Compare(span2, span3));
        Console.WriteLine("Result (Comparison of span1 and span3) = " + TimeSpan.Compare(span1, span3));
        Console.WriteLine("Result (Comparison of span3 and span4) = " + TimeSpan.Compare(span3, span4));
        Console.WriteLine("Result (Comparison of span4 and span5) = " + TimeSpan.Compare(span4, span5));
    }
}

The output of the above code is −

TimeSpan1 = 00:00:01.8768768
TimeSpan2 = -08:14:10
TimeSpan3 = 05:00:00
TimeSpan4 = 00:00:10
TimeSpan5 = 00:02:00
Result (Comparison of span1 and span2) = 1
Result (Comparison of span2 and span3) = -1
Result (Comparison of span1 and span3) = -1
Result (Comparison of span3 and span4) = 1
Result (Comparison of span4 and span5) = -1

Using Single Tick Values

Example

using System;

public class Demo {
    public static void Main() {
        TimeSpan span1 = TimeSpan.FromTicks(1);
        TimeSpan span2 = new TimeSpan(1);
        TimeSpan span3 = TimeSpan.FromHours(1);
        TimeSpan span4 = TimeSpan.FromMilliseconds(1);
        TimeSpan span5 = TimeSpan.FromMinutes(1);

        Console.WriteLine("TimeSpan1 = " + span1);
        Console.WriteLine("TimeSpan2 = " + span2);
        Console.WriteLine("TimeSpan3 = " + span3);
        Console.WriteLine("TimeSpan4 = " + span4);
        Console.WriteLine("TimeSpan5 = " + span5);
        Console.WriteLine("Result (Comparison of span1 and span2) = " + TimeSpan.Compare(span1, span2));
        Console.WriteLine("Result (Comparison of span2 and span3) = " + TimeSpan.Compare(span2, span3));
        Console.WriteLine("Result (Comparison of span1 and span3) = " + TimeSpan.Compare(span1, span3));
        Console.WriteLine("Result (Comparison of span3 and span4) = " + TimeSpan.Compare(span3, span4));
        Console.WriteLine("Result (Comparison of span4 and span5) = " + TimeSpan.Compare(span4, span5));
    }
}

The output of the above code is −

TimeSpan1 = 00:00:00.0000001
TimeSpan2 = 00:00:00.0000001
TimeSpan3 = 01:00:00
TimeSpan4 = 00:00:00.0010000
TimeSpan5 = 00:01:00
Result (Comparison of span1 and span2) = 0
Result (Comparison of span2 and span3) = -1
Result (Comparison of span1 and span3) = -1
Result (Comparison of span3 and span4) = 1
Result (Comparison of span4 and span5) = -1

Working with TimeSpan Constants

Example

using System;

public class Demo {
    public static void Main() {
        Console.WriteLine("Ticks per millisecond: " + TimeSpan.TicksPerMillisecond);
        Console.WriteLine("Ticks per second: " + TimeSpan.TicksPerSecond);
        Console.WriteLine("Ticks per minute: " + TimeSpan.TicksPerMinute);
        Console.WriteLine("Ticks per hour: " + TimeSpan.TicksPerHour);

        long oneSecondInTicks = TimeSpan.TicksPerSecond;
        TimeSpan oneSecond = TimeSpan.FromTicks(oneSecondInTicks);
        Console.WriteLine("One second using ticks: " + oneSecond);

        long halfSecondInTicks = TimeSpan.TicksPerSecond / 2;
        TimeSpan halfSecond = TimeSpan.FromTicks(halfSecondInTicks);
        Console.WriteLine("Half second using ticks: " + halfSecond);
    }
}

The output of the above code is −

Ticks per millisecond: 10000
Ticks per second: 10000000
Ticks per minute: 600000000
Ticks per hour: 36000000000
One second using ticks: 00:00:01
Half second using ticks: 00:00:00.5000000

Conclusion

The TimeSpan.FromTicks() method provides precise time measurement by working with ticks, where each tick represents 100 nanoseconds. This method is particularly useful when you need high-precision timing or when converting from other time measurement systems that work with tick-based values.

Updated on: 2026-03-17T07:04:36+05:30

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements