Home  |  Delphi .net Home  |  System.DateTime  |  Equals Method
  
Equals  
Method  
Returns true if DateTime values are exactly the same
DateTime Structure
System NameSpace
CF1.  Function Equals ( Target : Object ) : Boolean;
CF2.  Function Equals ( ValueA : DateTime; ValueB : DateTime ) : Boolean; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Syntax 1 compares the current (Source) DateTime value with the Target value :
 
Returns one of the following values :
 
Source < Target Gives <0
Source = Target Gives  0
Source > Target Gives >0

 
The Target parameter must be a DateTime object or a null reference. Quite why this parameter is not DateTime is not clear.
 
Syntax 2 compares ValueA with ValueB
 
Returns one of the following values :
 
ValueA < ValueB Gives <0
ValueA = ValueB Gives  0
ValueA > ValueB Gives >0

 
The exact value of the result for both syntaxes is not guaranteed.
 
The date and time must be exactly the same to the tick to give a 0 result. (A tick is 0.0000001 seconds).
Notes
Static methods are not methods of an object - they are simply class functions or procedures available at any time.
Microsoft MSDN Links
System
System.DateTime
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  dateTime1, dateTime2 : DateTime;

begin
  dateTime1 := Datetime.Create(2004, 6, 20);  // 00:00:00 on June 20 2004
  dateTime2 := Datetime.Create(2004, 6, 21);  // 00:00:00 on June 21 2004

  Console.WriteLine('dateTime1 = {0:F}', dateTime1);
  Console.WriteLine('dateTime2 = {0:F}', dateTime2);

  if dateTime1.Equals(dateTime2)
  then Console.WriteLine('dateTime1 =  dateTime2')
  else Console.WriteLine('dateTime1 <> dateTime2');

  Console.ReadLine;
end.
   dateTime1 = 20 June 2004 00:00:00
   dateTime2 = 21 June 2004 00:00:00
   dateTime1 <> dateTime2
Using the static method
program Project1;
{$APPTYPE CONSOLE}

var
  dateTime1, dateTime2 : DateTime;

begin
  dateTime1 := Datetime.Create(2004, 6, 20);  // 00:00:00 on June 20 2004
  dateTime2 := dateTime1.Date;                // Exact same date/time value

  Console.WriteLine('Starting with identical date/time values');
  Console.WriteLine;

  Console.WriteLine('dateTime1 = {0:F}', dateTime1);
  Console.WriteLine('dateTime2 = {0:F}', dateTime2);

  if System.DateTime.Equals(dateTime1, dateTime2)
  then Console.WriteLine('dateTime1 =  dateTime2')
  else Console.WriteLine('dateTime1 <> dateTime2');

  Console.WriteLine;
  Console.WriteLine('Adding just 1 tick to dateTime1');
  Console.WriteLine;

  dateTime1 := dateTime1.AddTicks(1);

  Console.WriteLine('The date/time values appear to be the same');
  Console.WriteLine('dateTime1 = {0:F}', dateTime1);
  Console.WriteLine('dateTime2 = {0:F}', dateTime2);

  if System.DateTime.Equals(dateTime1, dateTime2)
  then Console.WriteLine('dateTime1 =  dateTime2')
  else Console.WriteLine('dateTime1 <> dateTime2');

  Console.ReadLine;
end.
   Starting with identical date/time values
  
   dateTime1 = 20 June 2004 00:00:00
   dateTime2 = 20 June 2004 00:00:00
   dateTime1 =  dateTime2
  
   Adding just 1 tick to dateTime1
  
   The date/time values appear to be the same
   dateTime1 = 20 June 2004 00:00:00
   dateTime2 = 20 June 2004 00:00:00
   dateTime1 <> dateTime2
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author