DateTime Comparison Precision

20,619

Solution 1

What about using a timespan.

if (Math.Truncate((A - B).TotalMinutes) == 0)
{
    //There is less than one minute between them
}

Probably not the most elegant way, but it allows for cases which are one second apart and yet have different days/hours/minutes parts such as going over midnight.

Edit: it occured to me that the truncate is unecessary...

if (Math.Abs((A - B).TotalMinutes) < 1)
{
    //There is less than one minute between them
}

Personally I think this is more elegant...

Solution 2

One approach could be to create two new DateTimes from your values you want to compare, but ignore anything from the seconds on down and then compare those:

DateTime compare1 = new DateTime(year1, month1, day1, hour1, minute1, 0);
DateTime compare2 = new DateTime(year2, month2, day2, hour2, minute2, 0);

int result = DateTime.Compare(compare1, compare2);

I'd be the first to admit it's not elegant, but it solves the problem.

Solution 3

Using a TimeSpan you get all the granularity you want :

DateTime dt1, dt2;
double d = (dt2 - dt1).TotalDays;
double h = (dt2 - dt1).TotalHours;
double m = (dt2 - dt1).TotalMinutes;
double s = (dt2 - dt1).TotalSeconds;
double ms = (dt2 - dt1).TotalMilliseconds;
double ticks = (dt2 - dt1).Ticks;

Solution 4

public class DateTimeComparer : Comparer<DateTime>
{
    private Prescision _Prescision;

    public enum Prescision : sbyte
    {
        Millisecons,
        Seconds,
        Minutes,
        Hour,
        Day,
        Month,
        Year,
        Ticks
    }

    Func<DateTime, DateTime>[] actions = new Func<DateTime, DateTime>[]
        {
            (x) => { return x.AddMilliseconds(-x.Millisecond);},
            (x) => { return x.AddSeconds(-x.Second);},
            (x) => { return x.AddMinutes(-x.Minute);},
            (x) => { return x.AddHours(-x.Hour);},
            (x) => { return x.AddDays(-x.Day);},
            (x) => { return x.AddMonths(-x.Month);},
        };

    public DateTimeComparer(Prescision prescision = Prescision.Ticks)
    {
        _Prescision = prescision;
    }

    public override int Compare(DateTime x, DateTime y)
    {
        if (_Prescision == Prescision.Ticks)
        {
            return x.CompareTo(y);
        }

        for (sbyte i = (sbyte)(_Prescision - 1); i >= 0; i--)
        {
            x = actions[i](x);
            y = actions[i](y);
        }

        return x.CompareTo(y);
    }
}

Usage example:

new DateTimeComparer(DateTimeComparer.Prescision.Day).Compare(Date1, Date2)

Solution 5

You can convert them to String format and compare the string with each other.

This also gives freedom to choose your comparison parameters, like only the time without the date, etc.

if (String.Format("{0:ddMMyyyyHHmmss}", date1) == String.Format("{0:ddMMyyyyHHmmss}", date2))
{
     // success
}
Share:
20,619
akif
Author by

akif

Updated on July 23, 2022

Comments

  • akif
    akif almost 2 years

    I'm doing DateTime comparison but I don't want to do comparison at second, millisecond and ticks level. What's the most elegant way?

    If I simply compare the DateTime, then they are seldom equal due to ticks differences.