How to check if one DateTime is bigger than another in C #

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

+91
c #
Sep 18 '08 at 19:15
source share
12 answers
 if (StartDate < EndDate) // code 

if you only need dates, not time

 if (StartDate.Date < EndDate.Date) // code 
+180
Sep 18 '08 at 19:17
source share
 if(StartDate < EndDate) {} 

DateTime supports regular comparison operators.

+25
Sep 18 '08 at 19:17
source share
 if(dateTimeA > dateTimeB) Console.WriteLine("Do your own homework"); 
+22
Sep 18 '08 at 19:17
source share

You can use the overloaded <or>.

For example:

 DateTime d1 = new DateTime(2008, 1, 1); DateTime d2 = new DateTime(2008, 1, 2); if (d1 < d2) { ... 
+22
Sep 18 '08 at 19:17
source share
 if (StartDate>=EndDate) { throw new InvalidOperationException("Ack! StartDate is not before EndDate!"); } 
+8
Sep 18 '08 at 19:17
source share
 StartDate < EndDate 
+6
Sep 18 '08 at 19:17
source share

Check the DateTime.Compare Method

+5
Sep 18 '08 at 19:17
source share

It may be too late, but in order to help other people who might stumble upon this, I used the extension method using IComparable as follows:

 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 general and makes it suitable for use with a wide range of data types, not just dates.

You would use it as follows:

 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 } 
+4
Aug 20 '15 at 13:18
source share

I had the same requirement, but when using the accepted answer, he did not complete all my unit tests. The problem 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 a minimum date value of 01/01/0001) - this solution really passed 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 skips the edge where the start and end dates can be 01/01/0001, but that doesn't bother me.

+3
Jun 03 2018-12-12T00:
source share
  if (new DateTime(5000) > new DateTime(1000)) { Console.WriteLine("i win"); } 
+2
Sep 18 '08 at 19:18
source share

I would like to demonstrate that if you convert to .Date, you do not need to worry about hours / minutes / 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); } 
0
Apr 11 '17 at 14:55
source share

If you are working in ASP.NET, you need to compare the values โ€‹โ€‹between the two CalendarExtender objects, the date comparison method is slightly different, but still very identical.

 if(calStartDate.SelectedDate > calEndDate.SelectedDate) { MessageBox.ShowMessage("That not how time works."); } 

Where calStartDate and calEndDate are set as identifiers for your CalendarExtenders.

0
Jun 18 '19 at 12:29
source share



All Articles