Little Gems: ToShortDate Extension Method

I’m really surprised that the DateTime struct doesn’t have a way to compare if two dates are equal, disregarding the time.  To help that, I’ve created an extension method:


public static class DateTimeExtensions
	public static DateTime ToShortDate(this DateTime dateTime)
		return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);


With this you can compare dates without worrying about the time they were posted, which is useful when you want to group items into blocks of days.

Have a better way? Share it in the comments.

Update:  That didn’t take long. In the comments, Andy West points out that Stack Overflow has a question on this very subject: How do I compare dates in C#? Time for me to do some reverting.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.