---
title: "How to calculate the difference between Two Dates in C#?"  
description: "How to calculate the difference between Two Dates in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/forum/160701/how-to-calculate-the-difference-between-two-dates-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# How to calculate the difference between Two Dates in C#?

How to calculate the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between Two Dates in C#?

## Replies

### Reply by Ravi Vishwakarma

You can calculate the difference between two [dates](https://www.mindstick.com/interview/1691/different-between-datetime-now-and-datetime-utcnow) using the `DateTime` **structure** and its associated methods. The `DateTime` **structure** has a `Subtract` the method that returns a `TimeSpan` object representing the difference between two `DateTime` instances. The `TimeSpan` object provides properties to get the difference in days, hours, minutes, seconds, and more.

Here’s how you can calculate the difference between two dates:

## Example -

```cs
using System;

class Program
{
    static void Main()
    {
        // Define the start date (January 1, 2024)
        DateTime startDate = new DateTime(2024, 1, 1);

        // Get the current date and time
        DateTime endDate = DateTime.Now;

        // Calculate the difference between the end date and the start date
        TimeSpan difference = endDate - startDate;

		// Calculate the difference between the end date and the start date
        // TimeSpan difference = endDate.Subtract(startDate);

        // Output the difference in various units
        Console.WriteLine($"Difference in days: {difference.Days}"); // Difference in days
        Console.WriteLine($"Difference in hours: {difference.TotalHours}"); // Difference in total hours (including fractional hours)
        Console.WriteLine($"Difference in minutes: {difference.TotalMinutes}"); // Difference in total minutes (including fractional minutes)
        Console.WriteLine($"Difference in seconds: {difference.TotalSeconds}"); // Difference in total seconds (including fractional seconds)
    }
}
```

### Explanation

**Create** `DateTime` **Instances:** Create `DateTime` instances for the start date and end date. You can use the `DateTime` constructor to create specific dates or use properties like `DateTime.Now` for the current date and time.

**Calculate the Difference:** Subtract the start date from the end date using the `-` operator or the `Subtract` method. This returns a `TimeSpan` object.

**Access** `TimeSpan` **Properties:** Use properties of the `TimeSpan` object to get the difference in various units:

- `Days`: Gets the total number of whole days.
- `TotalHours`: Gets the total number of hours, including fractional hours.
- `TotalMinutes`: Gets the total number of minutes, including fractional minutes.
- `TotalSeconds`: Gets the total number of seconds, including fractional seconds.

**Read more**: [DateTime shorting in C#](https://www.mindstick.com/forum/2019/datetime-shorting-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160701/how-to-calculate-the-difference-between-two-dates-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
