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# Program to Subtract Two TimeSpan
In C#, the TimeSpan structure represents a time interval and provides methods to perform arithmetic operations. To subtract one TimeSpan from another, you can use the Subtract() method or the subtraction operator (-).
Syntax
Following is the syntax for subtracting TimeSpan objects using the Subtract() method −
TimeSpan result = timeSpan1.Subtract(timeSpan2);
Following is the syntax using the subtraction operator −
TimeSpan result = timeSpan1 - timeSpan2;
Using Subtract() Method
The Subtract() method returns a new TimeSpan that represents the difference between two TimeSpan values −
using System;
public class Demo {
public static void Main() {
TimeSpan t1 = TimeSpan.FromMinutes(2);
TimeSpan t2 = TimeSpan.FromMinutes(1);
Console.WriteLine("First TimeSpan: " + t1);
Console.WriteLine("Second TimeSpan: " + t2);
// Subtracting using Subtract() method
TimeSpan res = t1.Subtract(t2);
Console.WriteLine("Resultant TimeSpan: " + res);
}
}
The output of the above code is −
First TimeSpan: 00:02:00 Second TimeSpan: 00:01:00 Resultant TimeSpan: 00:01:00
Using Subtraction Operator
You can also use the subtraction operator (-) which provides a more concise syntax −
using System;
public class Demo {
public static void Main() {
TimeSpan startTime = new TimeSpan(10, 30, 0); // 10:30:00
TimeSpan endTime = new TimeSpan(8, 15, 30); // 08:15:30
Console.WriteLine("Start Time: " + startTime);
Console.WriteLine("End Time: " + endTime);
// Subtracting using - operator
TimeSpan duration = startTime - endTime;
Console.WriteLine("Duration: " + duration);
}
}
The output of the above code is −
Start Time: 10:30:00 End Time: 08:15:30 Duration: 02:14:30
Handling Negative Results
When the second TimeSpan is larger than the first, the result will be negative −
using System;
public class Demo {
public static void Main() {
TimeSpan smaller = TimeSpan.FromHours(1);
TimeSpan larger = TimeSpan.FromHours(3);
Console.WriteLine("Smaller TimeSpan: " + smaller);
Console.WriteLine("Larger TimeSpan: " + larger);
TimeSpan result = smaller - larger;
Console.WriteLine("Result (negative): " + result);
Console.WriteLine("Absolute value: " + result.Duration());
}
}
The output of the above code is −
Smaller TimeSpan: 01:00:00 Larger TimeSpan: 03:00:00 Result (negative): -02:00:00 Absolute value: 02:00:00
Comparison
| Method | Syntax | Usage |
|---|---|---|
| Subtract() method | t1.Subtract(t2) | More explicit, method-based approach |
| Subtraction operator | t1 - t2 | Cleaner, more readable syntax |
Conclusion
TimeSpan subtraction in C# can be performed using either the Subtract() method or the subtraction operator (-). Both approaches return a new TimeSpan representing the time difference, with negative results possible when subtracting a larger TimeSpan from a smaller one.
