[c#] How to check if one DateTime is greater than the other in C#

I have two DateTime objects: StartDate and EndDate. I want to make sure StartDate is before EndDate. How is this done in C#?

This question is related to c#

The answer is


StartDate < EndDate

if (StartDate>=EndDate)
{
    throw new InvalidOperationException("Ack!  StartDate is not before EndDate!");
}

if(StartDate < EndDate)
{}

DateTime supports normal comparision operators.


I'd like to demonstrate that if you convert to .Date that you don't need to worry about hours/mins/seconds etc:

    [Test]
    public void ConvertToDateWillHaveTwoDatesEqual()
    {
        DateTime d1 = new DateTime(2008, 1, 1);
        DateTime d2 = new DateTime(2008, 1, 2);
        Assert.IsTrue(d1 < d2);

        DateTime d3 = new DateTime(2008, 1, 1,7,0,0);
        DateTime d4 = new DateTime(2008, 1, 1,10,0,0);
        Assert.IsTrue(d3 < d4);
        Assert.IsFalse(d3.Date < d4.Date);
    }

        if (new DateTime(5000) > new DateTime(1000))
        {
            Console.WriteLine("i win");
        }

This is probably too late, but to benefit other people who might stumble upon this, I used an extension method do to this using IComparable like this:

public static class BetweenExtension
    {
        public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable
        {
            return (min.CompareTo(value) <= 0) && (value.CompareTo(max) <= 0);
        }
    }

Using this extension method with IComparable makes this method more generic and makes it usable with a wide variety of data types and not just dates.

You would use it like this:

DateTime start = new DateTime(2015,1,1);
DateTime end = new DateTime(2015,12,31);
DateTime now = new DateTime(2015,8,20);

if(now.IsBetween(start, end))
{
     //Your code here
}

You can use the overloaded < or > operators.

For example:

DateTime d1 = new DateTime(2008, 1, 1);
DateTime d2 = new DateTime(2008, 1, 2);
if (d1 < d2) { ...

I had the same requirement, but when using the accepted answer, it did not fulfill all of my unit tests. The issue for me is when you have a new object, with Start and End dates and you have to set the Start date ( at this stage your End date has the minimum date value of 01/01/0001) - this solution did pass all my unit tests:

    public DateTime Start
    {
        get { return _start; }
        set
        {
            if (_end.Equals(DateTime.MinValue))
            {
                _start = value;
            }
            else if (value.Date < _end.Date)
            {
                _start = value;
            }
            else
            {
                throw new ArgumentException("Start date must be before the End date.");
            }
        }
    }


    public DateTime End
    {
        get { return _end; }
        set
        {
            if (_start.Equals(DateTime.MinValue))
            {
                _end = value;
            }
            else if (value.Date > _start.Date)
            {
                _end = value;
            }
            else
            {
                throw new ArgumentException("End date must be after the Start date.");
            }
        }
    }

It does miss the edge case where both Start and End dates can be 01/01/0001 but I'm not concerned about that.


You can use the overloaded < or > operators.

For example:

DateTime d1 = new DateTime(2008, 1, 1);
DateTime d2 = new DateTime(2008, 1, 2);
if (d1 < d2) { ...

StartDate < EndDate

if (StartDate>=EndDate)
{
    throw new InvalidOperationException("Ack!  StartDate is not before EndDate!");
}

Check out DateTime.Compare method


You can use the overloaded < or > operators.

For example:

DateTime d1 = new DateTime(2008, 1, 1);
DateTime d2 = new DateTime(2008, 1, 2);
if (d1 < d2) { ...

Check out DateTime.Compare method


        if (new DateTime(5000) > new DateTime(1000))
        {
            Console.WriteLine("i win");
        }

if(StartDate < EndDate)
{}

DateTime supports normal comparision operators.


I'd like to demonstrate that if you convert to .Date that you don't need to worry about hours/mins/seconds etc:

    [Test]
    public void ConvertToDateWillHaveTwoDatesEqual()
    {
        DateTime d1 = new DateTime(2008, 1, 1);
        DateTime d2 = new DateTime(2008, 1, 2);
        Assert.IsTrue(d1 < d2);

        DateTime d3 = new DateTime(2008, 1, 1,7,0,0);
        DateTime d4 = new DateTime(2008, 1, 1,10,0,0);
        Assert.IsTrue(d3 < d4);
        Assert.IsFalse(d3.Date < d4.Date);
    }

I had the same requirement, but when using the accepted answer, it did not fulfill all of my unit tests. The issue for me is when you have a new object, with Start and End dates and you have to set the Start date ( at this stage your End date has the minimum date value of 01/01/0001) - this solution did pass all my unit tests:

    public DateTime Start
    {
        get { return _start; }
        set
        {
            if (_end.Equals(DateTime.MinValue))
            {
                _start = value;
            }
            else if (value.Date < _end.Date)
            {
                _start = value;
            }
            else
            {
                throw new ArgumentException("Start date must be before the End date.");
            }
        }
    }


    public DateTime End
    {
        get { return _end; }
        set
        {
            if (_start.Equals(DateTime.MinValue))
            {
                _end = value;
            }
            else if (value.Date > _start.Date)
            {
                _end = value;
            }
            else
            {
                throw new ArgumentException("End date must be after the Start date.");
            }
        }
    }

It does miss the edge case where both Start and End dates can be 01/01/0001 but I'm not concerned about that.


if (StartDate>=EndDate)
{
    throw new InvalidOperationException("Ack!  StartDate is not before EndDate!");
}

if(StartDate < EndDate)
{}

DateTime supports normal comparision operators.


Check out DateTime.Compare method


This is probably too late, but to benefit other people who might stumble upon this, I used an extension method do to this using IComparable like this:

public static class BetweenExtension
    {
        public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable
        {
            return (min.CompareTo(value) <= 0) && (value.CompareTo(max) <= 0);
        }
    }

Using this extension method with IComparable makes this method more generic and makes it usable with a wide variety of data types and not just dates.

You would use it like this:

DateTime start = new DateTime(2015,1,1);
DateTime end = new DateTime(2015,12,31);
DateTime now = new DateTime(2015,8,20);

if(now.IsBetween(start, end))
{
     //Your code here
}

if (StartDate>=EndDate)
{
    throw new InvalidOperationException("Ack!  StartDate is not before EndDate!");
}

Check out DateTime.Compare method


        if (new DateTime(5000) > new DateTime(1000))
        {
            Console.WriteLine("i win");
        }

StartDate < EndDate