How to compare DateTime in C #?

I do not want the user to specify a date or time.

How can I compare if the entered date and time is LESS, then the current time?

If the current date and time is June 17, 2010, 12:25 pm, I want the user to be unable to give the date until June 17 -2010 and the time until 12:25 pm.

Like my function, returns false if the time entered by the user is June 16, 2010 and the time is 12:24 pm

+119
c # datetime
Jun 17 '10 at 6:57
source share
6 answers

MSDN: DateTime.Compare

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0); int result = DateTime.Compare(date1, date2); string relationship; if (result < 0) relationship = "is earlier than"; else if (result == 0) relationship = "is the same time as"; else relationship = "is later than"; Console.WriteLine("{0} {1} {2}", date1, relationship, date2); // The example displays the following output: // 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM 
+159
Jun 17 '10 at 6:59
source share

Microsoft also implemented the operators '<' and '>'. Therefore, you use them to compare two dates.

 if (date1 < DateTime.Now) Console.WriteLine("Less than the current time!"); 
+282
Jun 17 '10 at 7:19
source share

MuSTaNG's answer says it all, but I'm still adding it to make it a little more complicated, with links and all.




Common operators

Available for DateTime with the .NET Framework 1.1. In addition, adding and subtracting DateTime objects is also possible using the usual + and - operators.

One example from MSDN:

Equality:
 System.DateTime april19 = new DateTime(2001, 4, 19); System.DateTime otherDate = new DateTime(1991, 6, 5); // areEqual gets false. bool areEqual = april19 == otherDate; otherDate = new DateTime(2001, 4, 19); // areEqual gets true. areEqual = april19 == otherDate; 

Other operators can be used in a similar way.

Below is a list of all the operators for DateTime .

+20
Oct 25 '16 at 6:55
source share

In general, you need to compare DateTimes with the same Kind :

 if (date1.ToUniversalTime() < date2.ToUniversalTime()) Console.WriteLine("date1 is earlier than date2"); 

An explanation from MSDN about DateTime.Compare (this also applies to operators such as > , < , == , etc.):

To determine the relationship of t1 to t2, the Compare method compares the Ticks property for t1 and t2, but ignores their Kind property . Before comparing DateTime objects, make sure the objects represent time in the same time zone.

Thus, a simple comparison may give an unexpected result when working with DateTimes , presented in different time zones.

+2
Mar 15 '19 at 15:03
source share
 //Time compare. private int CompareTime(string t1, string t2) { TimeSpan s1 = TimeSpan.Parse(t1); TimeSpan s2 = TimeSpan.Parse(t2); return s2.CompareTo(s1); } 
+1
Nov 04 '16 at 6:07
source share
 public static bool CompareDateTimes(this DateTime firstDate, DateTime secondDate) { return firstDate.Day == secondDate.Day && firstDate.Month == secondDate.Month && firstDate.Year == secondDate.Year; } 
-3
Jul 31 '18 at 8:27
source share



All Articles