settingsAccountsettings
Menusettings

Q: How to see the difference between dates in C#?

+4 votes

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#

asked in C# category by user matthew44
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

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);
    }
}
answered by user eiorgert
edited by user golearnweb
...