w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Difference between two dates in days

C# Sharp DateTime: Exercise-30 with Solution

Write a C# Sharp program to get the difference between two dates in days.

Sample Solution:-

C# Sharp Code:

using System;
class Example30
{
    public static void Main() 
    {
     //establish DateTimes
DateTime start = new DateTime(2010, 6, 14);
DateTime end = new DateTime(2016, 08, 14);

TimeSpan difference = end - start; //create TimeSpan object

Console.WriteLine("Difference in days: " + difference.Days); //Extract days, write to Console.
    }
}

Sample Output:

Difference in days: 2253  

C# Sharp Code Editor:

using System;
class Example30
{
    public static void Main() 
    {
     //establish DateTimes
DateTime start = new DateTime(2010, 6, 14);
DateTime end = new DateTime(2016, 08, 14);

TimeSpan difference = end - start; //create TimeSpan object

Console.WriteLine("Difference in days: " + difference.Days); //Extract days, write to Console.
    }
}

Improve this sample solution and post your code through Disqus