Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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.
