Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
Math.Max Method doesn't have overload for DateTime
So I've created

        public static DateTime Min(DateTime t1, DateTime t2)

        {

            if (DateTime.Compare(t1, t2) > 0)

            {

                return t2;

            }

            return t1;

        }

        public static DateTime Max(DateTime t1, DateTime t2)

        {

            if (DateTime.Compare(t1, t2) < 0)

            {

                return t2;

            }

            return t1;

        }

 

Posted on Thursday, February 21, 2008 4:50 PM .Net Framework | Back to top


Comments on this post: Min and Max methods for DateTime

# re: Min and Max methods for DateTime
Requesting Gravatar...
or you can do a one-line implementation:

Max(t1, t2):
return DateTime.Compare(t1, t2) > 0 ? t1 : t2;
Left by artykul8 on Oct 05, 2009 3:14 PM

# re: Min and Max methods for DateTime
Requesting Gravatar...
or you can try wrinting smth more universal

public static T Min<T>(T first, T second) where T : IComparable<T>
{
return first.CompareTo(second) > 0 ? second : first;
}
Left by Dmitry Seregin on Feb 09, 2010 4:17 AM

# re: Min and Max methods for DateTime
Requesting Gravatar...
How about this?

public static T Max<T>(T first, T second)
{
var comparer = Comparer<T>.Default;
return comparer.Compare(first, second) < 0 ? second : first;
}
Left by Kenneth Siewers Møller on Jun 29, 2010 4:01 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net