Share via


How to: Determine the Span Between Two Dates

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

Example

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;

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

Compiling the Code

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

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Date and Time Controls

Visual C# Guided Tour