date comparison in C#

  • Thread starter Thread starter Grey
  • Start date Start date
G

Grey

I need to compare two dates which one date is 4 days earlier than other date or I can customise the dates comparison difference. How to do it in C#??

Million Thanks
 
Hi Grey,

You use a TimeSpan for time comparison.

TimeSpan t = Date2 - Date1;

The TimeSpan class contains any number of different ways to show the
difference, including TotalDays, TotalSeconds, or as a combination of
Hours, Seconds, and so on.
 
Grey said:
I need to compare two dates which one date is 4 days earlier than
other date or I can customise the dates comparison difference. How to
do it in C#??

Use the overloaded subtraction operator:

DateTime d1 = ...;
DateTime d2 = ...;

TimeSpan difference = d1-d2;

then look at the information in the TimeSpan.
 
Back
Top