Export (0) Print
Expand All
Expand Minimize
26 out of 29 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

Show:
© 2014 Microsoft. All rights reserved.