In C# TimeSpan object represents a time interval, that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second, between two times.
TimeSpan can be used to compare two C# DateTime objects to find the difference between two dates.
Creating Timespan in C#
TimeSpan struct has the following overloaded forms:
//ticks: A time period expressed in 100-nanosecond units.
public TimeSpan(long ticks)
//pass hours, min, seconds elapsed
public TimeSpan(int hours,int minutes,int seconds)
//pass days, hours, min, seconds elapsed
public TimeSpan(int days,int hours,int minutes,int seconds)
//pass days, hours, min, seconds and milliseconds elapsed as argument
public TimeSpan(int days,int hours,int minutes,int seconds,int milliseconds)
So let's use any of the above TimeSpan instance constructors to create TimeSpan structs and then we will print the value of timespan
using System;
namespace CsharpTimeSpan
{
internal class Program
{
static void Main(string[] args)
{
// Use hours, minutes, seconds contructor
TimeSpan span = new TimeSpan(12,11,3);
Console.WriteLine(span);
}
}
}
Output:
12:11:03
Common usage of timespan is to get the difference between 2 dates in C#, so here is an example of doing it.
using System;
namespace CsharpTimeSpan
{
internal class Program
{
static void Main(string[] args)
{
DateTime d1 = new DateTime(2022,10,25);
DateTime d2 = new DateTime(2022, 10, 24);
//get difference between 2 dates
TimeSpan t = d1 - d2;
Console.WriteLine(t.TotalDays);
}
}
}
Output:
1
Since the difference between 2 of the above dates is 1 days, so output = 1
OR
Another example
DateTime departure = new DateTime(2022, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2022, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;
//total traveltime as difference between 2 datetime objects
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
Output:
13-06-2022 10:47:00 PM - 12-06-2022 06:32:00 PM = 1.04:15:00
Timespan Methods
TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, and milliseconds respectively.
- Add: Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
- Compare: Compares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value.
- FromDays: Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.
- FromHours: Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
- FromMicroseconds: Returns a TimeSpan that represents a specified number of microseconds.
- FromMilliseconds: Returns a TimeSpan that represents a specified number of milliseconds.
- FromMinutes: Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
- FromSeconds: Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
- FromTicks: Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.
There are many more methods for timespan, but we have mentioned only the one which is regularly used for TimeSpan.
Let's take a look at an example, using above methods.
using System;
namespace CsharpTimeSpan
{
internal class Program
{
static void Main(string[] args)
{
TimeSpan span1 = TimeSpan.FromMinutes(1);
TimeSpan span2 = TimeSpan.FromMinutes(2);
//add timespan
TimeSpan span3 = span1.Add(span2);
Console.WriteLine(span3);
//subtract timespan
TimeSpan span4 = span2.Subtract(span1);
Console.WriteLine(span4);
// Create TimeSpan using FromXXX methods
TimeSpan ts1 = TimeSpan.FromDays(10);
Console.WriteLine(ts1);
TimeSpan ts2 = TimeSpan.FromHours(8);
Console.WriteLine(ts2);
TimeSpan ts3 = TimeSpan.FromMinutes(20);
Console.WriteLine(ts3);
TimeSpan ts4 = TimeSpan.FromMilliseconds(2100);
Console.WriteLine(ts4);
}
}
}
Output:
00:03:00
00:01:00
10.00:00:00
08:00:00
00:20:00
00:00:02.1000000
The Add, Subtract, Multiply, Divide, and Negate methods to add, subtract, divide, multiply, and negate TimeSpan objects and Parse, ParseExact, TryParse, TryParseExact, TryFormat methods are used to parse and format TimeSpan objects into strings and vice versa.
You may also like to read:
HTML Comment Multiple lines (Block comments)