Checking if two dates are within 15 minutes of each other The following code compares two dates and checks if they are within 15 minutes of each other, with a tolerance of a second. All of the test cases below pass with the existing solution. The question is can it be improved by making it more efficient and/or elegant given the following test cases.

TestId, whenCreated, whenToCompareTo, expected Outcome

[(1, "01/01/2017 15:00:00", "01/01/2017 15:15:00", true)]
[(2, "01/01/2017 15:00:00", "01/01/2017 15:00:00", true)]
[(3, "01/01/2017 15:00:00", "01/01/2017 15:16:00", false)]
[(4, "01/01/2017 15:00:00", "01/01/2017 14:59:59", false)]
[(5, "01/01/2017 15:00:00", "01/01/2017 15:14:59", true)]
[(6, "01/01/2017 15:00:00", "01/01/2017 15:16:01", false)]
[(7, "2017-01-01T15:00:00.0000001", "2017-01-01T15:15:00.0000002", true)]
[(8, "2017-01-01T15:15:00.5170000", "2017-01-01T15:15:00.4537587", true)]
[(9, "2017-01-01 15:00:00", "2017-12-31 15:00:00", false)]
[(10, "2018-05-06T13:30:43.2200000", "2018-05-06T13:30:42.9978916", true)]


And here is the code that is doing the comparison:

public bool IsTokenAlive(DateTime timeToCompareTo)
{
    var isDateEarlier = DateComparison(timeToCompareTo, Created);
    var isCompareTimeOfDayEarlier = DateTimeOfDayComparison(timeToCompareTo, Created);
    var isExpired = AbsoluteSecondsComparison(timeToCompareTo, Created);
    var isAlive = isDateEarlier &amp;&amp; (isCompareTimeOfDayEarlier || GetTotalSeconds(timeToCompareTo, Created) &lt; 1) &amp;&amp; isExpired;

    return isAlive;
}

private static bool DateComparison(DateTime timeToCompareTo, DateTime created)
{
    return DateTime.Compare(timeToCompareTo.Date, created.Date) &gt;= 0;
}

private static bool DateTimeOfDayComparison(DateTime timeToCompareTo, DateTime created)
{
    //We are interested to compare up to the seconds, we are not interested in milliseconds
    return TimeSpan.Compare(StripMilliseconds(timeToCompareTo).TimeOfDay, StripMilliseconds(created).TimeOfDay) &gt;= 0;
}

private static bool AbsoluteSecondsComparison(DateTime timeToCompareTo, DateTime created)
{
    return GetTotalSeconds(timeToCompareTo, created) &lt; MaxTimeInSecondsTokenIsAlive;
}

private static DateTime StripMilliseconds(DateTime dateTime)
{
    return dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond));
}

private static double GetTotalSeconds(DateTime timeToCompareTo, DateTime created)
{
    return (timeToCompareTo - created).Duration().TotalSeconds;
}


The Created property is set when the object is created and it will always be the timestamp of the server.
Naming:

Method names should contain a verb and describe exactly what they are doing. For example, look at DateComparison. That sounds more like a type name to me, and I have no idea what the method will do by its name alone. 

StripMilliseconds and GetTotalSeconds are much better.



I'd suggest this is as an alternate solution:

public static class TokenValidator
{
    private const int aliveDurationMinutes = 15;

    /// &lt;summary&gt;
    /// Checks if the token is still alive with a 1-second tolerance.
    /// &lt;/summary&gt;
    public static bool CheckTokenAlive(DateTime created, DateTime timeToCompare)
    {
        TimeSpan elapsedTime = timeToCompare - created;

        return (elapsedTime.TotalSeconds &gt; -1 &amp;&amp; elapsedTime.TotalSeconds &lt; 0) // up to 1 second before
               || (elapsedTime.TotalSeconds &gt;= 0 &amp;&amp; Math.Floor(elapsedTime.TotalSeconds) &lt;= aliveDurationMinutes * 60); // up to 15 minutes later
    }
}


I use Math.Floor on the TotalSeconds to add the 1-second tolerance. You can be 0.9999 of a second over and still evaluate as true. You could have done a strictly less than with ((aliveDurationMinutes * 60) + 1) instead.