0
0
CsharpHow-ToBeginner · 4 min read

How to Use TimeSpan in C#: Syntax, Examples, and Tips

In C#, TimeSpan represents a time interval and can be created using constructors or static methods like TimeSpan.FromHours(). You can use it to add, subtract, or compare durations easily.
📐

Syntax

The TimeSpan structure represents a time interval. You can create it using constructors or static methods:

  • new TimeSpan(days, hours, minutes, seconds) creates a time span from days, hours, minutes, and seconds.
  • TimeSpan.FromHours(double hours) creates a time span from a fractional number of hours.
  • TimeSpan.FromMinutes(double minutes) creates a time span from minutes.

You can access properties like Hours, Minutes, and TotalSeconds to get parts or total duration.

csharp
TimeSpan ts1 = new TimeSpan(1, 2, 30, 0); // 1 day, 2 hours, 30 minutes, 0 seconds
TimeSpan ts2 = TimeSpan.FromHours(1.5); // 1 hour 30 minutes
💻

Example

This example shows how to create TimeSpan objects, add them, and display the result.

csharp
using System;

class Program
{
    static void Main()
    {
        TimeSpan time1 = new TimeSpan(1, 12, 30, 0); // 1 day, 12 hours, 30 minutes
        TimeSpan time2 = TimeSpan.FromMinutes(90); // 1 hour 30 minutes

        TimeSpan total = time1 + time2;

        Console.WriteLine($"Time1: {time1}");
        Console.WriteLine($"Time2: {time2}");
        Console.WriteLine($"Total Time: {total}");
        Console.WriteLine($"Total Hours: {total.TotalHours}");
    }
}
Output
Time1: 1.12:30:00 Time2: 01:30:00 Total Time: 1.14:00:00 Total Hours: 38
⚠️

Common Pitfalls

Common mistakes when using TimeSpan include:

  • Confusing Hours with TotalHours. Hours gives the hour part (0-23), while TotalHours gives the full duration in hours as a double.
  • Using the wrong constructor parameters order or forgetting that days are separate from hours.
  • Not handling negative TimeSpan values properly.
csharp
/* Wrong: Using Hours instead of TotalHours */
TimeSpan ts = TimeSpan.FromMinutes(150); // 2 hours 30 minutes
Console.WriteLine(ts.Hours); // Outputs 2 (only hour part)
Console.WriteLine(ts.TotalHours); // Outputs 2.5 (full duration)

/* Correct: Use TotalHours for full duration */
Output
2 2.5
📊

Quick Reference

Method/PropertyDescription
new TimeSpan(days, hours, minutes, seconds)Create TimeSpan from parts
TimeSpan.FromHours(double)Create TimeSpan from hours
TimeSpan.FromMinutes(double)Create TimeSpan from minutes
HoursHour component (0-23)
MinutesMinute component (0-59)
TotalHoursTotal duration in hours as double
TotalMinutesTotal duration in minutes as double
Add(TimeSpan)Add two TimeSpan values
Subtract(TimeSpan)Subtract one TimeSpan from another

Key Takeaways

Use TimeSpan to represent and manipulate time intervals easily in C#.
Create TimeSpan using constructors or static methods like FromHours or FromMinutes.
Use TotalHours or TotalMinutes to get the full duration, not just the component parts.
Be careful with constructor parameters and negative TimeSpan values.
You can add or subtract TimeSpan objects to calculate durations.