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. //Returns total interval in days.
  4. Console.WriteLine("Total Days = " + difference.TotalDays);
  5. //Returns hour interval leaving days.
  6. Console.WriteLine("Hours = " + difference.Hours);
  7. //Returns total interval in hours.
  8. Console.WriteLine("Total Hours = " + difference.TotalHours);
  9. //Returns the minute interval leaving hours and days.
  10. Console.WriteLine("Minutes = " + difference.Minutes);
  11. //Returns total interval in minutes.
  12. Console.WriteLine("Total Minutes = " + difference.TotalMinutes);
  13. //Returns the second interval leaving others.
  14. Console.WriteLine("Seconds = " + difference.Seconds);
  15. //Returns total interval in seconds.
  16. Console.WriteLine("Total Seconds =" + difference.TotalSeconds);
  17. //Returns the millisecond interval leaving others.
  18. Console.WriteLine("Milliseconds = " + difference.Milliseconds);
  19. //Returns total interval in milliseconds.
  20. Console.WriteLine("Total Milliseconds =" + difference.TotalMilliseconds);
  21. //Returns total number of ticks.
  22. //Ticks is basically Milliseconds * 10,000
  23. 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.