Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare a date in C# to "1/1/0001 12:00:00 AM")

Tags:

c#

People also ask

How do you compare a date?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you compare two date functions?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

Can we compare string date?

To compare two dates, you can use either toString() or valueOf() . The toString() method converts the date into an ISO date string, and the valueOf() method converts the date into milliseconds since the epoch.


You can use DateTime.MinValue, which has exactly the same value:

if (e.CreatedDate == DateTime.MinValue) 

To check if it equals the default, you can use the default keyword:

if (e.CreatedDate == default(DateTime))

"1/1/0001 12:00:00 AM" this is a string datatype. so convert it to DateTime.

if (e.CreatedDate == Convert.ToDateTime("1/1/0001 12:00:00 AM"))
{
    //--- To Dos
}

But .NET Framework provide a default way to check this using

if (e.CreatedDate.Equals(DateTime.MinValue))

MSDN


use DateTime.MinValue

if (e.CreatedDate.Equals(DateTime.MinValue))

I assume e.CreateDate is of type DateTime.

Try it like:

if (e.CreatedDate == DateTime.Parse("1/1/0001 12:00:00 AM"))

If your aiming to compare if it's equal to the minimum date then just compare it to DateTime.MinValue. The above example is more generic.


You should use DateTime.Compare(DateTime, DateTime) function, which returns an integer. Eg.

if (DateTime.Compare(DateTime.MinValue, e.CreatedDate) == 0){
   ...
}

You can check the default value with DateTime.MinValue:

if (e.CreatedDate == DateTime.MinValue)