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.Subtract() Method in C#
The TimeSpan.Subtract() method in C# returns a new TimeSpan object whose value is the difference between the current instance and the specified TimeSpan object. This method is useful for calculating time differences in applications dealing with durations and intervals.
Syntax
Following is the syntax for the TimeSpan.Subtract() method −
public TimeSpan Subtract(TimeSpan span);
Parameters
-
span − The
TimeSpanobject to subtract from the current instance.
Return Value
Returns a new TimeSpan object that represents the time interval difference. If the subtracted value is greater than the current instance, the result will be negative.
Using TimeSpan.Subtract() Method
Example 1
Let us see a basic example demonstrating subtraction of TimeSpan objects −
using System;
public class Demo {
public static void Main() {
TimeSpan span1 = TimeSpan.FromMinutes(30);
TimeSpan span2 = TimeSpan.FromMinutes(10);
TimeSpan span3 = TimeSpan.FromHours(2);
Console.WriteLine("TimeSpan1 = " + span1);
Console.WriteLine("TimeSpan2 = " + span2);
Console.WriteLine("TimeSpan3 = " + span3);
TimeSpan result1 = span1.Subtract(span2);
TimeSpan result2 = span3.Subtract(span1);
TimeSpan result3 = span2.Subtract(span3);
Console.WriteLine("span1 - span2 = " + result1);
Console.WriteLine("span3 - span1 = " + result2);
Console.WriteLine("span2 - span3 = " + result3);
}
}
The output of the above code is −
TimeSpan1 = 00:30:00 TimeSpan2 = 00:10:00 TimeSpan3 = 02:00:00 span1 - span2 = 00:20:00 span3 - span1 = 01:30:00 span2 - span3 = -01:50:00
Example 2
Here's an example showing subtraction with different time units −
using System;
public class Demo {
public static void Main() {
TimeSpan hours = TimeSpan.FromHours(3);
TimeSpan minutes = TimeSpan.FromMinutes(45);
TimeSpan seconds = TimeSpan.FromSeconds(30);
TimeSpan milliseconds = TimeSpan.FromMilliseconds(500);
Console.WriteLine("Hours: " + hours);
Console.WriteLine("Minutes: " + minutes);
Console.WriteLine("Seconds: " + seconds);
Console.WriteLine("Milliseconds: " + milliseconds);
TimeSpan result1 = hours.Subtract(minutes);
TimeSpan result2 = minutes.Subtract(seconds);
TimeSpan result3 = seconds.Subtract(milliseconds);
Console.WriteLine("3 hours - 45 minutes = " + result1);
Console.WriteLine("45 minutes - 30 seconds = " + result2);
Console.WriteLine("30 seconds - 500 milliseconds = " + result3);
}
}
The output of the above code is −
Hours: 03:00:00 Minutes: 00:45:00 Seconds: 00:00:30 Milliseconds: 00:00:00.5000000 3 hours - 45 minutes = 02:15:00 45 minutes - 30 seconds = 00:44:30 30 seconds - 500 milliseconds = 00:00:29.5000000
Example 3
This example demonstrates subtracting TimeSpan objects with precise tick values −
using System;
public class Demo {
public static void Main() {
TimeSpan span1 = TimeSpan.FromTicks(1500);
TimeSpan span2 = new TimeSpan(2, 40, 55);
TimeSpan span3 = TimeSpan.FromHours(5);
Console.WriteLine("TimeSpan1 (ticks) = " + span1);
Console.WriteLine("TimeSpan2 (2h 40m 55s) = " + span2);
Console.WriteLine("TimeSpan3 (5 hours) = " + span3);
TimeSpan result1 = span2.Subtract(span1);
TimeSpan result2 = span3.Subtract(span2);
TimeSpan result3 = span1.Subtract(span2);
Console.WriteLine("span2 - span1 = " + result1);
Console.WriteLine("span3 - span2 = " + result2);
Console.WriteLine("span1 - span2 = " + result3);
}
}
The output of the above code is −
TimeSpan1 (ticks) = 00:00:00.0001500 TimeSpan2 (2h 40m 55s) = 02:40:55 TimeSpan3 (5 hours) = 05:00:00 span2 - span1 = 02:40:54.9998500 span3 - span2 = 02:19:05 span1 - span2 = -02:40:54.9998500
Conclusion
The TimeSpan.Subtract() method provides an easy way to calculate the difference between two time intervals. The result can be positive or negative depending on which TimeSpan is larger, making it useful for duration calculations and time arithmetic in C# applications.
