TimeSpan.From methods in C# ()

The TimeSpan.From methods in C# provide a convenient way to create TimeSpan objects from specific time units. These static methods include FromDays, FromHours, FromMinutes, FromSeconds, FromMilliseconds, and FromTicks.

Each method takes a numeric value and returns a TimeSpan representing that duration in the specified unit. This approach is more readable than manually calculating ticks or using TimeSpan constructors.

Syntax

Following are the syntax formats for the most commonly used TimeSpan.From methods −

TimeSpan.FromDays(double value)
TimeSpan.FromHours(double value)
TimeSpan.FromMinutes(double value)
TimeSpan.FromSeconds(double value)
TimeSpan.FromMilliseconds(double value)
TimeSpan.FromTicks(long value)

Parameters

All TimeSpan.From methods (except FromTicks) accept a double parameter representing the number of units. FromTicks accepts a long parameter representing the number of ticks (100-nanosecond intervals).

Return Value

Each method returns a TimeSpan object representing the specified duration.

TimeSpan.From Methods Hierarchy FromDays FromHours FromMinutes FromSeconds FromMilliseconds FromTicks All return TimeSpan objects Input: numeric values ? Output: TimeSpan duration

Using TimeSpan.From Methods

Example

using System;

public class Demo {
   public static void Main() {
      TimeSpan t1 = TimeSpan.FromDays(1);
      TimeSpan t2 = TimeSpan.FromHours(1);
      TimeSpan t3 = TimeSpan.FromMinutes(1);
      TimeSpan t4 = TimeSpan.FromSeconds(30);
      TimeSpan t5 = TimeSpan.FromMilliseconds(500);
      
      Console.WriteLine("1 Day: " + t1);
      Console.WriteLine("1 Hour: " + t2);
      Console.WriteLine("1 Minute: " + t3);
      Console.WriteLine("30 Seconds: " + t4);
      Console.WriteLine("500 Milliseconds: " + t5);
   }
}

The output of the above code is −

1 Day: 1.00:00:00
1 Hour: 01:00:00
1 Minute: 00:01:00
30 Seconds: 00:00:30
500 Milliseconds: 00:00:00.5000000

Using Fractional Values

Example

using System;

public class Demo {
   public static void Main() {
      TimeSpan t1 = TimeSpan.FromDays(1.5);
      TimeSpan t2 = TimeSpan.FromHours(2.25);
      TimeSpan t3 = TimeSpan.FromMinutes(90.5);
      
      Console.WriteLine("1.5 Days: " + t1);
      Console.WriteLine("2.25 Hours: " + t2);
      Console.WriteLine("90.5 Minutes: " + t3);
   }
}

The output of the above code is −

1.5 Days: 1.12:00:00
2.25 Hours: 02:15:00
90.5 Minutes: 01:30:30

Comparison of TimeSpan.From Methods

Method Input Type Description Example Usage
FromDays double Creates TimeSpan from days TimeSpan.FromDays(7)
FromHours double Creates TimeSpan from hours TimeSpan.FromHours(24)
FromMinutes double Creates TimeSpan from minutes TimeSpan.FromMinutes(60)
FromSeconds double Creates TimeSpan from seconds TimeSpan.FromSeconds(3600)
FromMilliseconds double Creates TimeSpan from milliseconds TimeSpan.FromMilliseconds(1000)
FromTicks long Creates TimeSpan from ticks TimeSpan.FromTicks(10000000)

Conclusion

TimeSpan.From methods provide an intuitive way to create TimeSpan objects from specific time units. These methods accept both whole and fractional values, making them versatile for various time calculations and more readable than manual TimeSpan constructor usage.

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

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements