TimeSpan and Durations
Measure spans of time.
TimeSpan and Durations is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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);
}
}Factory Methods
Often it reads better to use a factory method. TimeSpan.FromMinutes(90) builds a span of 90 minutes.
There are matching helpers: FromHours, FromSeconds, FromDays, and FromMilliseconds.
using System;
class Program
{
static void Main()
{
TimeSpan span = TimeSpan.FromMinutes(90);
Console.WriteLine(span);
}
}Total vs Component Values
TimeSpan has two families of properties. Hours, Minutes, and Seconds are the broken-down components.
The Total... versions like TotalMinutes express the whole duration in one unit, as a decimal.
using System;
class Program
{
static void Main()
{
TimeSpan span = TimeSpan.FromMinutes(90);
Console.WriteLine(span.Hours);
Console.WriteLine(span.TotalMinutes);
}
}Component vs Total Explained
For 90 minutes, Hours is 1 and Minutes is 30, because the duration breaks into 1 hour and 30 minutes.
But TotalHours is 1.5 and TotalMinutes is 90, since those count the entire span in a single unit.
using System;
class Program
{
static void Main()
{
TimeSpan span = TimeSpan.FromMinutes(90);
Console.WriteLine(span.TotalHours);
}
}Subtracting Two Dates
When you subtract one DateTime from another, the result is a TimeSpan.
This is the everyday way to find how much time passed between two moments, such as the length of an event.
using System;
class Program
{
static void Main()
{
DateTime start = new DateTime(2024, 3, 5, 9, 0, 0);
DateTime end = new DateTime(2024, 3, 5, 11, 30, 0);
TimeSpan diff = end - start;
Console.WriteLine(diff.TotalHours);
}
}Adding TimeSpans
You can add or subtract TimeSpan values with the normal + and - operators.
Combining two durations gives a longer one, which is useful for summing several intervals.
using System;
class Program
{
static void Main()
{
TimeSpan a = TimeSpan.FromMinutes(45);
TimeSpan b = TimeSpan.FromMinutes(15);
Console.WriteLine((a + b).TotalMinutes);
}
}Negative Durations
A TimeSpan can be negative if the end is before the start, or after a subtraction.
The Duration method returns the absolute value, and Negate flips the sign. These help when you only care about magnitude.
using System;
class Program
{
static void Main()
{
TimeSpan span = TimeSpan.FromHours(-3);
Console.WriteLine(span.Duration().TotalHours);
}
}Comparing Durations
TimeSpan values compare with <, >, and == just like numbers.
This lets you check whether one duration is longer than another, for example to enforce a maximum allowed time.
using System;
class Program
{
static void Main()
{
TimeSpan limit = TimeSpan.FromHours(2);
TimeSpan used = TimeSpan.FromMinutes(150);
Console.WriteLine(used > limit);
}
}Zero and Constants
TimeSpan.Zero represents no elapsed time, a handy starting point for sums.
There are also TimeSpan.MinValue and TimeSpan.MaxValue for the extreme bounds of a duration.
using System;
class Program
{
static void Main()
{
TimeSpan total = TimeSpan.Zero;
total += TimeSpan.FromMinutes(10);
Console.WriteLine(total.TotalMinutes);
}
}Formatting a TimeSpan
You can format a TimeSpan with a custom string. For example @"hh\:mm" shows hours and minutes.
The backslashes escape the colon so it appears literally. For quick output, the default ToString is usually enough.
using System;
class Program
{
static void Main()
{
TimeSpan span = new TimeSpan(2, 5, 0);
Console.WriteLine(span.ToString(@"hh\:mm"));
}
}Quick Check
You have a TimeSpan of 90 minutes. Which property returns the value 1.5?
Recap
A TimeSpan is a duration. Build it with new TimeSpan(...) or factory methods like FromMinutes.
Component properties (Hours) differ from Total... properties. Subtracting two DateTimes yields a TimeSpan, and you can add, compare, and format durations.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “TimeSpan and Durations” lesson free?
Yes — the full text of “TimeSpan and Durations” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “TimeSpan and Durations”?
Measure spans of time. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “TimeSpan and Durations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating DateTimes
- Formatting Dates
- TimeSpan and Durations
- Date Math