Learn About TimeSpan In C#

TimeSpan is a class in C#, used for time interval operations.

TimeSpan class can be instantiated by any one of the following methods,

Simple Object Creation with no parameters
  1. TimeSpan ts = new TimeSpan();  
  2. Console.WriteLine(ts.ToString());  
This creates an empty TimeSpan object with zero value.

By passing hours, minutes and seconds.
  1. TimeSpan ts1 = new TimeSpan(23, 10, 10);  
By passing days, hours, minutes and seconds.
  1. TimeSpan ts2 = new TimeSpan(10, 23, 10, 10); 
By passing days, hours, minutes, seconds and milliseconds.
  1. TimeSpan ts3 = new TimeSpan(10, 23, 10, 10, 10);  
By passing number of ticks.
  1. TimeSpan ts4 = new TimeSpan(100);  

TimeSpan in C#

Why TimeSpan? Is it necessary when we already have “DateTime” Class for Date & Time operations?

TimeSpan is used to get the interval between two DateTime values. You can get the interval difference in TimeSpan, Days, Hours, Minutes, Seconds, Milliseconds, Ticks.

  1. DateTime startDateTime = DateTime.Now;  
  2. DateTime endDateTime = DateTime.Now.AddDays(10);  
  3. TimeSpan difference = endDateTime - startDateTime;  
  4. Console.WriteLine("Difference from \n {0} \n and \n {1} \n = \n  
  5. {2}",endDateTime, startDateTime, difference);  
TimeSpan in C#

TimeSpan Components

  1.  //Returns the day interval.  
  2. Console.WriteLine("Days = " + difference.Days);             
  3.    
  4. //Returns total interval in days.  
  5. Console.WriteLine("Total Days = " + difference.TotalDays);             
  6.    
  7. //Returns hour interval leaving days.  
  8. Console.WriteLine("Hours = " + difference.Hours);             
  9.    
  10. //Returns total interval in hours.  
  11. Console.WriteLine("Total Hours = " + difference.TotalHours);  
  12.    
  13. //Returns the minute interval leaving hours and days.  
  14. Console.WriteLine("Minutes = " + difference.Minutes);             
  15.    
  16. //Returns total interval in minutes.  
  17. Console.WriteLine("Total Minutes = " + difference.TotalMinutes);         
  18.    
  19. //Returns the second interval leaving others.  
  20. Console.WriteLine("Seconds = " + difference.Seconds);             
  21.    
  22. //Returns total interval in seconds.  
  23. Console.WriteLine("Total Seconds =" + difference.TotalSeconds);             
  24.    
  25. //Returns the millisecond interval leaving others.  
  26. Console.WriteLine("Milliseconds = " + difference.Milliseconds);  
  27.    
  28. //Returns total interval in milliseconds.  
  29. Console.WriteLine("Total Milliseconds =" + difference.TotalMilliseconds);   
  30.    
  31. //Returns total number of ticks.  
  32. //Ticks is basically Milliseconds * 10,000  
  33. Console.WriteLine("Ticks = " + difference.Ticks);  
TimeSpan in C#

FIELDS

TimeSpan has many static fields that can be accessed directly.

  • To get Maximum Value
    TimeSpan.MaxValue

  • To get Minimum Value
    TimeSpan.MinValue

  • To assign a null object or zero ticks
    TimeSpan.Zero

 We can get the number of Ticks in a day/hour/minute/second/millisecond. This value is a constant.

  • TimeSpan.TicksPerDay
  • TimeSpan.TicksPerHour
  • TimeSpan.TicksPerMinute
  • TimeSpan.TicksPerSecond
  • TimeSpan.TicksPerMillisecond

So, technically Ticks is 10,000 times a Millisecond.

TimeSpan in C#

Conclusion

So, we have seen how to create a TimeSpan object and how useful it will be to us. I hope you have enjoyed the article.