TimeSpan in C#

This blog explains use of C# TimeSpan with code examples.

C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.
 

Creating TimeSpan

 
TimeSpan struct has the following overloaded forms:
 
  1. TimeSpan(Int32, Int32, Int32)  
  2. public TimeSpan(int hours,int minutes,int seconds)

  3. TimeSpan(Int32, Int32, Int32, Int32)  
  4. public TimeSpan(int days,int hours,int minutes,int seconds)  

  5. TimeSpan(Int32, Int32, Int32, Int32, Int32)  
  6. public TimeSpan(int days,int hours,int minutes,int seconds,int milliseconds)  

  7. TimeSpan(Int64)  
  8. public TimeSpan(long ticks)  
The following code snippet crates a TimeSpan from days, hours, and minutes. 
  1. // Create a TimeSpan  
  2. TimeSpan interval = new TimeSpan(5, 6, 22);  
  3. Console.WriteLine(interval.ToString());  
The common method of creating a TimeSpan is finding the difference between two DateTime objects, The following code snippet is example of getting the interval between two DateTime objects. 

TimeSpan Properties

 
C# TimeSpan class properties are Days, Hours, Minutes, Seconds, Milliseconds, and Ticks that returns days, hours, minutes, seconds, and milliseconds in a TimeSpan object. The TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds properies return the totals of them on an object.
 
The following code snippet gets a TimeSpan between two dates and reads these properties.
  1. // Lauch date of C# Corner Conference 2020 is 3/15/2020. 9:00 AM.  
  2. // Find out how may days, minutes, seconds to the conference from now  
  3. DateTime launchDate = new DateTime(2020, 3, 15, 9, 0, 0);  
  4. DateTime now = DateTime.Now;  
  5. // Calculate the interval between the two dates.  
  6. TimeSpan ts = launchDate - now;  
  7. Console.WriteLine("TimeSpan: {0}", ts.ToString());  
  8. // TimeSpan properties  
  9. Console.WriteLine("Days: {0}", ts.Days);  
  10. Console.WriteLine("Total Number of Days {0}:", ts.TotalDays);  
  11. Console.WriteLine("Hours: {0}", ts.Hours);  
  12. Console.WriteLine("Total number of hours: {0}", ts.TotalHours);  
  13. Console.WriteLine("Minutes: {0}", ts.Minutes);  
  14. Console.WriteLine("Total Number of Minutes: {0}", ts.TotalMinutes);  
  15. Console.WriteLine("Seconds: {0}", ts.Seconds);  
  16. Console.WriteLine("Total Number of Seconds: {0}", ts.TotalSeconds);  
  17. Console.WriteLine("Milliseconds: {0}", ts.Milliseconds);  
  18. Console.WriteLine("Total Number of Milliseconds: {0}", ts.TotalMilliseconds);  
  19. Console.WriteLine("Ticks: {0}", ts.Ticks);  
 

TimeSpan Methods

 
TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, and milliseconds respectively.
 
The following code snippet adds TimeSpan objects using the Fromxxx methods. 
  1. // Create TimeSpan Fromxxx methods  
  2. TimeSpan ts1 = TimeSpan.FromDays(12);  
  3. TimeSpan ts2 = TimeSpan.FromHours(8);  
  4. TimeSpan ts3 = TimeSpan.FromMinutes(20);  
  5. TimeSpan ts4 = TimeSpan.FromMilliseconds(2300);  
The Add, Subtract, Multiply, Divide, and Negate methods to adds, subtract, divide, multiply, and negate TimeSpan objects.
 
The following code snippet is an example of how to add and subtract TimeSpan objects. 
  1. TimeSpan ts5 = ts1.Add(ts2);  
  2. Console.WriteLine(ts5);  
  3. TimeSpan ts6 = ts5.Subtract(ts2);  
  4. Console.WriteLine(ts6);  
The Parse, ParseExact, TryParse, TryParseExact, and TryFormat methods are used to parse and format TimeSpan objects into strings and vice versa.
 

Summary

 
In this article and code examples, we saw how to use a TimeSpan class, its methods, and properties to work with time intervals.