c sharp

C# TimeSpan

The C# TimeSpan type specifies the difference in time between two times stated in days, hours, minutes, and seconds, this signifies the relations of time. C# TimeSpan is used for the comparison of two C# DateTime objects to detect the difference between two dates. To get the interval between two DateTime values, we use TimeSpan. It is a C# class that is used to perform operations at time intervals.

Properties of TimeSpan in the C# program

The TimeSpan in C# programming has the following properties:

  • Days are used to return days in a TimeSpan object.
  • Hours are used to return hours.
  • Minutes are used to return minutes.
  • Seconds are used to return seconds.
  • Milliseconds are used to return milliseconds.

To get the total of all the above:

TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds are used.

Example 1: Using the new operator in C# programming in Ubuntu 20.04

The simplest way of creating TimeSpan is by creating a “new operator” in the C# program. Here the code is specified in the image below:

In the example, we import first the “using system” for including namespaces. Then, we created a class named “Span”. Next is, we invoked the main function. Inside this main function, we create a TimeSpan object “time1” without parameters with the help of the keyword “new”.  Then, display this on the screen by using “Console.WrireLine” it will print the empty TimeSpan on the output screen.

After this, we pass some parameters to the TimeSpan. We create a new TimeSpan “time 2” and pass parameters up to seconds “TimeSpan(10, 15, 82)”. Display also this TimeSpan with the help of “Console.WriteLine()”. We have to display TimeSpan for days to seconds. For this, we create a new Timespan “time3” and pass parameters “TimeSpan(19, 11, 63, 42)”. We call the “ConsoleWriteLine(time3)” function to display “time3”. It will display days, hours, minutes, and seconds.

After this, we create one more Timespan “time4” for displaying days, hours, minutes, seconds, and also milliseconds. By using “new TimeSpan(1, 11, 56, 42, 19)”, “time4” is created. We also want to print this so we use the “ConsoleWriteLine()” method. This will show TimeSpan from days to milliseconds.

For output
We will get the output of the C# program in Ubuntu 20.04 by typing the commands given below. First, the code is compiled with the help of the “mcs” command and then generates a file with the extension “.exe”. Now, we will execute that code by using the “mono” command in Ubuntu 20.04 terminal.

In this output, the first display is all zero because we didn’t pass any parameters in the code as we create an empty “TimeSpan”. The next line shows hours, minutes, and seconds as we pass these parameters in “Timespan”. After this, it displays days, hours, minutes, and seconds and at the last line, it displays “TimeSpan” from days to milliseconds as we have already created in the code.

Example 2: Using Properties of TimeSpan in C#

Here, we have given you an example, in which we use the “properties of TimeSpan” for displaying days, total hours, total minutes, total seconds, and total milliseconds in C# programming in Ubuntu 20.04.

In the above code, the first line of code has a “using System” library. Then, we have a class with the name “Detail” and this class contains the main function which is “static void main()”. Inside this main function, the object of “TimeSpan” is created with the name “myDays”. The “new” is a keyword used for creating TimeSpan as we explain in the previous example. We have to pass the parameters to this TimeSpan.

After this, we want to display all the details about the parameter which we pass in the “TimeSpan” such as days, total hours, total minutes, total seconds, and total milliseconds. For this, we have to use the properties of TimeSpan. Now for printing days, we invoke the method “Console.WriteLine()”. Inside this, we display the days by using the property “Days” as shown in the image above. This will display the days. After this, we use the “TotalDays” property in the “Console.WriteLine” method which will display the total hours in “10 days”.

After displaying the total hours, we are going to display the total minutes in “10 days” using “Console.WriteLine(“Total Minutes =” + myDays.TotalMinutes)”. Here, “TotalMinutes” is the property of “TimeSpan” which is used for displaying total minutes in “10 days”. After displaying minutes, display seconds and milliseconds. By using the same method, we will display the seconds and milliseconds. For showing the seconds, we use the “Totalseconds” property which will display the total seconds in “10 days”. After this, we use the property “Totalmilliseconds” to display all milliseconds in “10 days”.

We will get the output of the above C# code in Ubuntu 20.04 in the same way as shown in the image below.

Example 3: Using AddDays() Method in C# in Ubuntu 20.04

Here, we have an example in the image below which uses the “AddDays() method” in TimeSpan in C# programming.

In the overhead code, we use the system library “using System” first. This gives access to classes and functions in C# programming. We declare a class with the name “Difference”. Inside the class, we invoked the main function. After this, we used the “DateTime” type. It is a value type used for the representation of dates and times in the C# program. We declared and initialized “startDT” with the value type of “DateTime”. Then, assigned “DateTime.Now” to this “startDT” which means that it assigns the current date and time to “startDT”.

As we know “DateTime.Now” is a property in C# to return the current date and time. After this, we are declare and initialize “endDT” with the same value type that is “DateTime”. We assign “DateTime.Now.AddDays(15)” to “endDT” which means we add “15” days to the current date and time. The “AddDays()” is a method in C# programming that we use to add the days. Now we use the TimeSpan structure which is used for finding the difference between two times. We use a TimeSpan struct with the name “diff” and get the difference between “enddDT-startDT”. It will return the difference between the endDT and startDT and assign this time to “diff”.

We use the method “Console.WriteLine” to display this time on screen. By using “Console.WriteLine(Difference from \n {0} \n and \n {1} \n = \n {2}”, endDT, startDT, diff},  we will print startDT, endDT and the difference. As we know “\n” is used for a new line.

We will get the output that is given in the image below. In this output, we can see that the date and time are displayed first and then the difference between the endDT, and startDT is displayed in “TimeSpan”.


In this guide, we explore many examples of “TimeSpan” in C # programming. This “TimeSpan” is very helpful in representing the period in the C# program. It provides many methods and properties to calculate time. We have methods to add and subtract the “TimeSpan” of our choice. We also find maximum and minimum values by using MaxValue, and MinValue methods. There are many other methods available which are very helpful for us and more convenient. We have explained a few examples here in this guide which are very interesting and helpful for understanding the concept of “TimeSpan”.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.