Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. TimeSpan / Date Difference Calculation

TimeSpan / Date Difference Calculation

The TimeSpan type represents a duration or time interval, and shows how to calculate the difference between two DateTime values by subtracting one from the other.

Syntax

// Subtract two DateTime values to get the difference.
TimeSpan diff = dateTime2 - dateTime1;

// Create a TimeSpan directly.
TimeSpan ts = new TimeSpan(int hours, int minutes, int seconds);
TimeSpan ts = new TimeSpan(int days, int hours, int minutes, int seconds);

// Create a TimeSpan using static methods.
TimeSpan ts = TimeSpan.FromDays(double value);
TimeSpan ts = TimeSpan.FromHours(double value);
TimeSpan ts = TimeSpan.FromMinutes(double value);

Member List

MemberDescription
.DaysReturns the whole-number days component of the interval (fractional part is truncated).
.HoursReturns the hours component (0–23).
.MinutesReturns the minutes component (0–59).
.SecondsReturns the seconds component (0–59).
.TotalDaysReturns the total duration expressed as days (including fractional days).
.TotalHoursReturns the total duration expressed as hours (including fractional hours).
.TotalMinutesReturns the total duration expressed as minutes (including fractional minutes).
TimeSpan.FromDays(n)Creates a TimeSpan of n days.

Sample Code

using System;

// Calculate the difference between two DateTime values.
DateTime start = new DateTime(2024, 1, 1, 9, 0, 0);
DateTime end   = new DateTime(2024, 1, 3, 17, 30, 0);

TimeSpan diff = end - start;
Console.WriteLine($"Days: {diff.Days}");              // 2
Console.WriteLine($"Hours: {diff.Hours}");            // 8
Console.WriteLine($"Minutes: {diff.Minutes}");        // 30
Console.WriteLine($"Total hours: {diff.TotalHours}"); // 56.5
Console.WriteLine($"Total minutes: {diff.TotalMinutes}"); // 3390

// Measure elapsed time for a piece of code.
DateTime measureStart = DateTime.Now;
// ...some processing...
System.Threading.Thread.Sleep(100); // Wait 100 ms (for demonstration)
TimeSpan elapsed = DateTime.Now - measureStart;
Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds:F0} ms");

// Create a TimeSpan directly.
TimeSpan workTime = new TimeSpan(8, 30, 0); // 8 hours 30 minutes
Console.WriteLine($"Work time: {workTime.TotalHours} hours");

// Calculate the number of days until a deadline.
DateTime deadline = new DateTime(2024, 12, 31);
TimeSpan remaining = deadline - DateTime.Today;
Console.WriteLine($"Days until deadline: {(int)remaining.TotalDays}");

Notes

TimeSpan is a type that represents a length of time. .Days returns only the days component of the interval, whereas .TotalDays returns the entire interval converted to days as a decimal value. Use whichever property suits your needs.

For measuring code execution time, the System.Diagnostics.Stopwatch class is a better fit. Start it with Stopwatch.StartNew() and read the elapsed time via stopwatch.Elapsed.

For formatting and adding to date/time values, see DateTime.ToString() / DateTime.AddDays().

If you find any errors or copyright issues, please .