TimeSpan.FromSeconds() Method in C#

The TimeSpan.FromSeconds() method in C# is used to return a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond. This static method is particularly useful when you need to create time intervals based on second values.

Syntax

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

public static TimeSpan FromSeconds(double value);

Parameters

  • value − A number of seconds, accurate to the nearest millisecond. Can be positive, negative, or zero.

Return Value

Returns a TimeSpan object that represents the specified number of seconds.

TimeSpan.FromSeconds() Conversion Input 90.5 seconds TimeSpan Output 00:01:30.5000000 FromSeconds() Converts seconds to Hours:Minutes:Seconds format Accurate to nearest millisecond

Using TimeSpan.FromSeconds() with Decimal Values

Example

using System;

public class Demo {
    public static void Main() {
        TimeSpan span1 = TimeSpan.FromSeconds(90.5);
        TimeSpan span2 = TimeSpan.FromSeconds(3661.250);
        TimeSpan span3 = TimeSpan.FromSeconds(0.001);
        
        Console.WriteLine("90.5 seconds = " + span1);
        Console.WriteLine("3661.25 seconds = " + span2);
        Console.WriteLine("0.001 seconds = " + span3);
        
        Console.WriteLine("\nDetailed breakdown:");
        Console.WriteLine("span2 Hours: " + span2.Hours);
        Console.WriteLine("span2 Minutes: " + span2.Minutes);
        Console.WriteLine("span2 Seconds: " + span2.Seconds);
        Console.WriteLine("span2 Milliseconds: " + span2.Milliseconds);
    }
}

The output of the above code is −

90.5 seconds = 00:01:30.5000000
3661.25 seconds = 01:01:01.2500000
0.001 seconds = 00:00:00.0010000

Detailed breakdown:
span2 Hours: 1
span2 Minutes: 1
span2 Seconds: 1
span2 Milliseconds: 250

Using TimeSpan.FromSeconds() with Negative Values

Example

using System;

public class Demo {
    public static void Main() {
        TimeSpan positiveSpan = TimeSpan.FromSeconds(120);
        TimeSpan negativeSpan = TimeSpan.FromSeconds(-120);
        TimeSpan zeroSpan = TimeSpan.FromSeconds(0);
        
        Console.WriteLine("Positive 120 seconds: " + positiveSpan);
        Console.WriteLine("Negative 120 seconds: " + negativeSpan);
        Console.WriteLine("Zero seconds: " + zeroSpan);
        
        Console.WriteLine("\nComparisons:");
        Console.WriteLine("Positive vs Negative: " + TimeSpan.Compare(positiveSpan, negativeSpan));
        Console.WriteLine("Negative vs Zero: " + TimeSpan.Compare(negativeSpan, zeroSpan));
        Console.WriteLine("Positive vs Zero: " + TimeSpan.Compare(positiveSpan, zeroSpan));
    }
}

The output of the above code is −

Positive 120 seconds: 00:02:00
Negative 120 seconds: -00:02:00
Zero seconds: 00:00:00

Comparisons:
Positive vs Negative: 1
Negative vs Zero: -1
Positive vs Zero: 1

Common Use Cases

Use Case Example Result
Timer delays TimeSpan.FromSeconds(5) 00:00:05
API timeouts TimeSpan.FromSeconds(30) 00:00:30
Cache expiration TimeSpan.FromSeconds(3600) 01:00:00
Millisecond precision TimeSpan.FromSeconds(1.5) 00:00:01.5000000

Conclusion

The TimeSpan.FromSeconds() method provides an easy way to create TimeSpan objects from second values with millisecond precision. It accepts positive, negative, and fractional values, making it versatile for various timing scenarios in applications.

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

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements