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
C# TimeSpan Min value
The TimeSpan structure in C# represents a time interval or duration. The TimeSpan.MinValue property returns the minimum possible value that a TimeSpan can represent, which is approximately -10.7 million days.
This minimum value is useful when you need to initialize a TimeSpan variable to the smallest possible value or when performing comparisons to find the minimum time span in a collection.
Syntax
Following is the syntax to access the minimum TimeSpan value −
TimeSpan.MinValue
Return Value
The TimeSpan.MinValue property returns a TimeSpan object representing the minimum possible time interval, which equals approximately -10,675,199 days, 2 hours, 48 minutes, 5 seconds, and 4,775,808 ticks.
Example
The following example demonstrates how to use TimeSpan.MinValue −
using System;
public class Demo {
public static void Main() {
Console.WriteLine("TimeSpan MinValue: " + TimeSpan.MinValue);
Console.WriteLine("Total Days: " + TimeSpan.MinValue.TotalDays);
Console.WriteLine("Total Hours: " + TimeSpan.MinValue.TotalHours);
Console.WriteLine("Ticks: " + TimeSpan.MinValue.Ticks);
}
}
The output of the above code is −
TimeSpan MinValue: -10675199.02:48:05.4775808 Total Days: -10675199.116730064 Total Hours: -256204778.80152154 Ticks: -9223372036854775808
Using MinValue for Comparisons
The following example shows how to use TimeSpan.MinValue to find the shortest duration from a collection −
using System;
public class Demo {
public static void Main() {
TimeSpan[] durations = {
TimeSpan.FromHours(5),
TimeSpan.FromMinutes(30),
TimeSpan.FromSeconds(45),
TimeSpan.FromDays(2)
};
TimeSpan shortest = TimeSpan.MaxValue;
foreach (TimeSpan duration in durations) {
if (duration < shortest) {
shortest = duration;
}
}
Console.WriteLine("Shortest duration: " + shortest);
Console.WriteLine("MinValue for comparison: " + TimeSpan.MinValue);
Console.WriteLine("Is shortest greater than MinValue? " + (shortest > TimeSpan.MinValue));
}
}
The output of the above code is −
Shortest duration: 00:00:45 MinValue for comparison: -10675199.02:48:05.4775808 Is shortest greater than MinValue? True
Comparison with MaxValue
| Property | Value | Description |
|---|---|---|
| TimeSpan.MinValue | -10675199.02:48:05.4775808 | Minimum possible TimeSpan value (negative) |
| TimeSpan.MaxValue | 10675199.02:48:05.4775807 | Maximum possible TimeSpan value (positive) |
| TimeSpan.Zero | 00:00:00 | Represents zero time interval |
Conclusion
The TimeSpan.MinValue property provides the smallest possible TimeSpan value, representing approximately -10.7 million days. It is commonly used for initialization and comparison operations when working with time intervals in C# applications.
