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.Add() Method in C#
The TimeSpan.Add() method in C# returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and the current instance. This method is useful for adding time intervals together, such as combining durations or calculating elapsed time periods.
Syntax
Following is the syntax for the TimeSpan.Add() method −
public TimeSpan Add(TimeSpan span);
Parameters
span − A TimeSpan object representing the time interval to add to the current TimeSpan instance.
Return Value
Returns a new TimeSpan object that represents the sum of the current TimeSpan and the specified TimeSpan parameter. The original TimeSpan objects remain unchanged.
Using TimeSpan.Add() with Positive Values
The following example demonstrates adding positive TimeSpan values −
using System;
public class Demo {
public static void Main() {
TimeSpan span1 = new TimeSpan(2, 17, 20, 30);
TimeSpan span2 = new TimeSpan(3, 18, 25, 25);
TimeSpan span3 = new TimeSpan(-8, 30, 0);
TimeSpan res1 = span1.Add(span2);
TimeSpan res2 = span2.Add(span3);
Console.WriteLine("TimeSpan1: " + span1);
Console.WriteLine("TimeSpan2: " + span2);
Console.WriteLine("TimeSpan3: " + span3);
Console.WriteLine("Final Timespan (TimeSpan1 + TimeSpan2) = " + res1);
Console.WriteLine("Final Timespan (TimeSpan2 + TimeSpan3) = " + res2);
}
}
The output of the above code is −
TimeSpan1: 2.17:20:30 TimeSpan2: 3.18:25:25 TimeSpan3: -08:30:00 Final Timespan (TimeSpan1 + TimeSpan2) = 6.11:45:55 Final Timespan (TimeSpan2 + TimeSpan3) = 3.10:55:25
Using TimeSpan.Add() with Extreme Values
The following example shows adding TimeSpan values including the minimum possible value −
using System;
public class Demo {
public static void Main() {
TimeSpan span1 = new TimeSpan(-6, 25, 0);
TimeSpan span2 = new TimeSpan(1, 11, 25, 20);
TimeSpan span3 = TimeSpan.MinValue;
TimeSpan res1 = span1.Add(span2);
TimeSpan res2 = span2.Add(span3);
Console.WriteLine("TimeSpan1: " + span1);
Console.WriteLine("TimeSpan2: " + span2);
Console.WriteLine("TimeSpan.MinValue: " + span3);
Console.WriteLine("Final Timespan (TimeSpan1 + TimeSpan2) = " + res1);
Console.WriteLine("Final Timespan (TimeSpan2 + TimeSpan.MinValue) = " + res2);
}
}
The output of the above code is −
TimeSpan1: -06:25:00 TimeSpan2: 1.11:25:20 TimeSpan.MinValue: -10675199.02:48:05.4775808 Final Timespan (TimeSpan1 + TimeSpan2) = 1.05:00:20 Final Timespan (TimeSpan2 + TimeSpan.MinValue) = -10675197.15:22:45.4775808
Common Use Cases
-
Calculating total elapsed time by adding multiple time intervals
-
Adding processing delays or timeouts to existing time periods
-
Combining work hours, break times, or project durations
-
Performing time arithmetic in scheduling applications
Conclusion
The TimeSpan.Add() method provides a straightforward way to add time intervals in C#. It returns a new TimeSpan object representing the sum while leaving the original TimeSpan instances unchanged, making it safe for immutable time calculations.
