TimeSpan and Durations
Measure spans of time.
What Is a TimeSpan
While DateTime is a point in time, TimeSpan represents a length of time, like 2 hours or 90 minutes.
It is a duration with no calendar attached. You use it for stopwatch results, timeouts, and the gap between two dates.
Creating a TimeSpan
One constructor takes hours, minutes, and seconds: new TimeSpan(hours, minutes, seconds).
So new TimeSpan(1, 30, 0) is one hour and thirty minutes. The value prints in a clear h:mm:ss layout.
using System;
class Program
{
static void Main()
{
TimeSpan span = new TimeSpan(1, 30, 0);
Console.WriteLine(span);
}
}All lessons in this course
- Creating DateTimes
- Formatting Dates
- TimeSpan and Durations
- Date Math