TimeSpan in C#

Introduction

C# TimeSpan struct represents a time interval that is the difference between two times measured in a 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.

public TimeSpan(int hours, int minutes, int seconds)
{
    // Constructor: TimeSpan(Int32, Int32, Int32)
}

public TimeSpan(int days, int hours, int minutes, int seconds)
{
    // Constructor: TimeSpan(Int32, Int32, Int32, Int32)
}

public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
{
    // Constructor: TimeSpan(Int32, Int32, Int32, Int32, Int32)
}

public TimeSpan(long ticks)
{
    // Constructor: TimeSpan(Int64)
}

The following code snippet creates a TimeSpan from days, hours, and minutes. 

// Create a TimeSpan  
TimeSpan interval = new TimeSpan(5, 6, 22);  
Console.WriteLine(interval.ToString());  

The common method of creating a TimeSpan is finding the difference between two DateTime objects. The following code snippet is an 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 properties return the totals of them on an object.

The following code snippet gets a TimeSpan between two dates and reads these properties.

// Lauch date of C# Corner Conference 2020 is 3/15/2020, 9:00 AM.
// Find out how many days, minutes, and seconds to the conference from now.
DateTime launchDate = new DateTime(2020, 3, 15, 9, 0, 0);
DateTime now = DateTime.Now;

// Calculate the interval between the two dates.
TimeSpan ts = launchDate - now;

Console.WriteLine("TimeSpan: {0}", ts.ToString());

// TimeSpan properties
Console.WriteLine("Days: {0}", ts.Days);
Console.WriteLine("Total Number of Days: {0}", ts.TotalDays);
Console.WriteLine("Hours: {0}", ts.Hours);
Console.WriteLine("Total number of hours: {0}", ts.TotalHours);
Console.WriteLine("Minutes: {0}", ts.Minutes);
Console.WriteLine("Total Number of Minutes: {0}", ts.TotalMinutes);
Console.WriteLine("Seconds: {0}", ts.Seconds);
Console.WriteLine("Total Number of Seconds: {0}", ts.TotalSeconds);
Console.WriteLine("Milliseconds: {0}", ts.Milliseconds);
Console.WriteLine("Total Number of Milliseconds: {0}", ts.TotalMilliseconds);
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. 

// Create TimeSpan Fromxxx methods  
TimeSpan ts1 = TimeSpan.FromDays(12);  
TimeSpan ts2 = TimeSpan.FromHours(8);  
TimeSpan ts3 = TimeSpan.FromMinutes(20);  
TimeSpan ts4 = TimeSpan.FromMilliseconds(2300);  

The Add, Subtract, Multiply, pide, and Negate methods to add, subtract, pide, multiply, and negate TimeSpan objects.

The following code snippet is an example of how to add and subtract TimeSpan objects. 

TimeSpan ts5 = ts1.Add(ts2);  
Console.WriteLine(ts5);  
TimeSpan ts6 = ts5.Subtract(ts2);  
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.