This post will discuss how to convert a TimeSpan object to a formatted string in C#.

A TimeSpan object represents a time interval unrelated to a particular date. It differs from the DateTime object, which represents both date and time values.

We can get a TimeSpan object by subtracting two DateTime objects in C#. The following example prints a TimeSpan object.

Download  Run Code

 
To do formatting of a TimeSpan object, use TimeSpan.ToString() method. Have a look at the Microsoft Documentation on TimeSpan custom format strings for more information.

Download  Run Code

 
The TimeSpan object exposes its members to return the total number of days, hours, minutes, seconds, and fractions of a second. The following example displays properties of a TimeSpan object that represents the difference between two dates.

Download  Run Code

That’s all about converting a TimeSpan to a formatted string in C#.