CompareWithoutMinutes
Compares two datettime objects ignoring minutes and seconds
Source
public static int CompareWithoutMinutes(this DateTime source, DateTime toCompare)
{
source = new DateTime(source.Year, source.Month, source.Day, source.Hour, 0, 0);
toCompare = new DateTime(toCompare.Year, toCompare.Month, toCompare.Day, toCompare.Hour, 0, 0);
return source.CompareTo(toCompare);
}
Example
public class Program
{
public static void Main()
{
Console.WriteLine(DateTime.Now.AddMinutes(2).CompareWithoutMinutes(DateTime.Now) == 0); //prints true
Console.WriteLine(DateTime.Now.AddHours(2).CompareWithoutMinutes(DateTime.Now) == 0); //prints false
}
}