HomeCSharpHow to calculate difference between two dates in C# ?

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...