w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Difference between two dates in days

Secondary Nav

C# Sharp DateTime : Exercise-30 with Solution

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

Expected Output :

Difference in days: 2253   

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.
    }
}

C# Sharp Practice online:

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.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number