简体   繁体   中英

Compare nullable datetime objects

I have two nullable datetime objects, I want to compare both. What is the best way to do it?

I have already tried:

DateTime.Compare(birthDate, hireDate);

This is giving an error, maybe it is expecting dates of type System.DateTime and I have Nullable datetimes.

I have also tried:

birthDate > hiredate...

But the results are not as expected...any suggestions?

To compare two Nullable<T> objects use Nullable.Compare<T> like:

bool result = Nullable.Compare(birthDate, hireDate) > 0;

You can also do:

Use the Value property of the Nullable DateTime. (Remember to check if both object Has some values)

if ((birthDate.HasValue && hireDate.HasValue) 
    && DateTime.Compare(birthDate.Value, hireDate.Value) > 0)
{
}

If both values are Same DateTime.Compare will return you 0

Something Like

DateTime? birthDate = new DateTime(2000, 1, 1);
DateTime? hireDate = new DateTime(2013, 1, 1);
if ((birthDate.HasValue && hireDate.HasValue) 
    && DateTime.Compare(birthDate.Value, hireDate.Value) > 0)
{
}

Nullable.Equals Indicates whether two specified Nullable(Of T) objects are equal.

Try:

if(birthDate.Equals(hireDate))

The best way would be: Nullable.Compare Method

Nullable.Compare(birthDate, hireDate));

If you want a null value to be treated as default(DateTime) you could do something like this:

public class NullableDateTimeComparer : IComparer<DateTime?>
{
    public int Compare(DateTime? x, DateTime? y)
    {
        return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault());
    }
}

and use it like this

var myComparer = new NullableDateTimeComparer();
myComparer.Compare(left, right);

Another way to do this would be to make an extension method for Nullable types whose values are comparable

public static class NullableComparableExtensions
{
    public static int CompareTo<T>(this T? left, T? right)
        where T : struct, IComparable<T>
    {
        return left.GetValueOrDefault().CompareTo(right.GetValueOrDefault());
    }
}

Where you'd use it like this

DateTime? left = null, right = DateTime.Now;
left.CompareTo(right);

Use the Nullable.Compare<T> method. Like this:

var equal = Nullable.Compare<DateTime>(birthDate, hireDate);

As @Vishal stated, simply use overriden Equals method of Nullable<T> . It is implemented this way:

public override bool Equals(object other)
{
    if (!this.HasValue)    
        return (other == null);

    if (other == null)    
        return false;

    return this.value.Equals(other);
}

It returns true if either both nullable structs do not have value, or if their values are equal. So, simply use

birthDate.Equals(hireDate)

Try

birthDate.Equals(hireDate)

and do your stuff after comparison.

Or, use

object.equals(birthDate,hireDate)

我认为您可以按以下方式使用该条件

birthdate.GetValueOrDefault(DateTime.MinValue) > hireddate.GetValueOrDefault(DateTime.MinValue)

You can write a generic method to calculate Min or Max for any type like below:

public static T Max<T>(T FirstArgument, T SecondArgument) {
    if (Comparer<T>.Default.Compare(FirstArgument, SecondArgument) > 0)
        return FirstArgument;
    return SecondArgument;
}

Then use like below:

var result = new[]{datetime1, datetime2, datetime3}.Max();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM