How to Calculate the Difference Between Dates in C#: Number of Days

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How many days have passed? Solving the eternal problem in C#

\n\n

Imagine: you are writing a vacation tracking app, a task scheduler, or just want to know how many days are left until the New Year. Then the classic question arises: "How to calculate the difference between two dates in C#?". This query is one of the most popular on Stack Overflow, and for good reason. Working with dates is a fundamental skill every developer needs to know.

\n\n

In this article, we won't just give a dry answer. We'll break it down with real-life examples, show you the pitfalls, and teach you how to get the number of days (and more) between any two points in time. Let's go!

\n\n

The simplest way: subtract one date from another

\n\n

In C#, there is a powerful structure DateTime for working with dates and times. The main secret is that if you subtract one DateTime object from another, you don't get a number, but a special object — TimeSpan. It stores the difference.

\n\n

Basic example: difference in days between two specific dates

\n\n

Let's say you want to know how many days passed between March 1, 2024 and May 10, 2024.

\n\n
using System;\n\nclass Program\n{\n    static void Main()\n    {\n        // Create two dates\n        DateTime startDate = new DateTime(2024, 3, 1);\n        DateTime endDate = new DateTime(2024, 5, 10);\n\n        // Calculate the difference\n        TimeSpan difference = endDate - startDate;\n\n        // Get the number of whole days\n        int days = difference.Days;\n\n        Console.WriteLine($"Difference in days: {days}");\n        // Output: Difference in days: 70\n    }\n}\n
\n\n

Simple, isn't it? The - operator is overloaded for DateTime and returns a TimeSpan. The .Days property returns the whole number of days (without considering hours, minutes, and seconds).

\n\n

Calculating the difference from "today"

\n\n

Often you need to calculate how many days have passed from some event to the current moment. For this, use DateTime.Now or DateTime.Today.

\n\n
DateTime today = DateTime.Today; // Date only, time 00:00\nDateTime someDay = new DateTime(2025, 1, 1); // January 1, 2025\n\nTimeSpan diff = someDay - today;\n\nConsole.WriteLine($"Days until New Year: {diff.Days} days");\n
\n\n

Note: DateTime.Today returns the current date with the time set to 00:00:00. This is convenient if you don't care about hours and minutes.

\n\n

What if you need more than just days?

\n\n

The TimeSpan structure is not just about .Days. It has other useful properties that will come in handy if you decide to expand the functionality.

\n\n
    \n
  • .Days — the number of whole days.
  • \n
  • .Hours — hours (from 0 to 23).
  • \n
  • .Minutes — minutes.
  • \n
  • .Seconds — seconds.
  • \n
  • .TotalDays — the total number of days as a fractional number (e.g., 70.5).
  • \n
  • .TotalHours — the total number of hours.
  • \n
\n\n

Example with TotalDays

\n\n

Imagine the difference between dates is exactly 36 hours. The .Days property will show 1 (because it's one whole day and 12 hours). But .TotalDays will show 1.5.

\n\n
DateTime start = new DateTime(2024, 1, 1, 0, 0, 0);\nDateTime end = new DateTime(2024, 1, 2, 12, 0, 0); // Jan 1 + 1 day and 12 hours\n\nTimeSpan span = end - start;\n\nConsole.WriteLine($"Days (whole): {span.Days}");       // 1\nConsole.WriteLine($"Total days: {span.TotalDays}");    // 1.5\n
\n\n

If your task is simply to find out the number of full days, use .Days. If you need an exact value with fractions, use

Blogs

Book Recommendations