Expand Minimize
23 out of 25 rated this helpful - Rate this topic

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.

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;

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

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.