;

How to Get Total Number of Days between Two Dates in C#


Tutorialsrack 09/12/2020 C#

In this article, you will learn how to get the total number of days between two dates in c#. In this example, we create the TimeSpan object to calculate the difference between the two Dates.

Here is the Example to get the total no. of days between two dates in C#.

Code - To Get the Total No. of Days Between Two Date in C#.
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Get Total Number of Days between Two Dates in C# */
        static void Main(string[] args)
        {
            DateTime startDate = new DateTime(2019, 08, 14);
            DateTime endDate = new DateTime(2020, 08, 13);

            //create TimeSpan object
            TimeSpan difference = endDate - startDate;

            // Extract days
            // Note: It will show 1 day less in total Days
            //       because it will include the start date Day
            //       but not include the endDate day(Excluded Date: 2020-08-13)
            Console.WriteLine("Total Difference in days Between Two Dates: " + difference.TotalDays);

            // If you want EndDate day will also include
            // in totaldays then add 1 to TotalDays
            Console.WriteLine("Total Difference in days Between Two Dates(After Including End Date Day): " + (difference.TotalDays+1));
            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output

Total Difference in days Between Two Dates: 365

Total Difference in days Between Two Dates(After Including End Date Day): 366

I hope this article will help you to understand how to get the total number of days between two dates in c#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags