[Solved] How to see the difference between dates in C#?

  

3
Topic starter

Write a program that enters two dates in format dd.MM.yyyy and returns the number of days between them.

Examples:

difference between time in c#

1 Answer
2

First read more about TimeSpan Structure HERe:  https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx

and here is the code:

using System;
 
class DifferenceBetweenDates
{
    static void Main()
    {
        DateTime firstDate = DateTime.Parse(Console.ReadLine());
        DateTime secondDate = DateTime.Parse(Console.ReadLine());
 
        TimeSpan dif = secondDate - firstDate;//Class wich represents a time interval.
        int days = dif.Days;//.Days is a property of the class TimeSpan which shows the days
 
        Console.WriteLine("{0}", days);
    }
}
Share: