How to calculate difference between two dates in C# ?

There are times when you might to calculate the difference between two days in C# and get the number of days as result.

How to calculate difference between two dates in C# ?

Assuming both the start date and the end date are of type DateTime , we can use the TotalDays property to find the number of days between two days.

using System;

namespace DeveloperPublish
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime date1 = new DateTime(2015, 1, 18);
            DateTime date2 = new DateTime(2015, 1, 11);
            // Find the difference between two dates and get the Total Days
            double NoOfDays = (date1 - date2).TotalDays;
            Console.WriteLine(NoOfDays);
            Console.ReadLine();
        }
    }
}


Leave a Reply