I see a lot of code that requires one to compare two date, date and time, or time only values. I think there may be as many ways to skin this cat as there are developers given the number of examples I’ve seen. This I know, the proper way to compare date (or datetime) data types in .Net is to use Date.Compare like so:
The result is documented on MSDN which states:
Less than zero = first date is earlier than the second.
Zero = first date is the same as the second.
Greater than zero = first date is later than the second.
The thing that seems to catch some guys is that the above works just fine with Date or DateTime data types that only contain hours, minutes, seconds such as the following variable:
Of course, there is no year, month, day resolution in using a variable value such as this. A comparison can only tell you how the HH:MM:SS value of one variable compares to another.
Leave a Reply