Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one DateTime is greater than the other in C#

Tags:

c#

People also ask

How do I check if one date is greater than another in C#?

Compare() Method in C# This method is used to compare two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. Syntax: public static int Compare (DateTime t1, DateTime t2);

How do you check if a date is before another date in python?

Check if One DateTime is Less than other DateTime You can use less than comparison operator < to check if one datetime object is less than other.


if (StartDate < EndDate)
   // code

if you just want the dates, and not the time

if (StartDate.Date < EndDate.Date)
    // code

if(StartDate < EndDate)
{}

DateTime supports normal comparision operators.


You can use the overloaded < or > operators.

For example:

DateTime d1 = new DateTime(2008, 1, 1);
DateTime d2 = new DateTime(2008, 1, 2);
if (d1 < d2) { ...

if (StartDate>=EndDate)
{
    throw new InvalidOperationException("Ack!  StartDate is not before EndDate!");
}