gray laptop computer showing html codes in shallow focus photography

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 find the number of days between two dates in C#. This blog will explain how you can do that easily.

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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...