This documentation is archived and is not being maintained.

Code: Determining the Span Between Two Dates (Visual C#)

Visual Studio .NET 2003

This example calculates the difference in days between two dates and constructs a TimeSpan value for that difference.


DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;

Console.WriteLine("Difference in days: {0} ", differenceInDays);

Compiling the Code

Copy the code and paste it into the Main method of a console application.

See Also

TimeSpan Structure | DateTime Members | DateTime Subtraction Operator | DateTime.Subtract Method| TimeSpan.Days Property | General Language Example Topics