Min and Max methods for DateTime

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;

        }

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Thursday, February 21, 2008 4:50 PM
Print

Comments on this entry:

# re: Min and Max methods for DateTime

Left by artykul8 at 10/5/2009 3:14 PM
Gravatar
or you can do a one-line implementation:

Max(t1, t2):
return DateTime.Compare(t1, t2) > 0 ? t1 : t2;

# re: Min and Max methods for DateTime

Left by Dmitry Seregin at 2/9/2010 4:17 AM
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;
}

# re: Min and Max methods for DateTime

Left by Kenneth Siewers Møller at 6/29/2010 4:01 PM
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;
}

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910