Home  |  Delphi .net Home  |  System.DateTime  |  Compare Method
  
Compare  
Method  
Compares two DateTime values
DateTime Structure
System NameSpace
CF1.  Function Compare ( ValueA : DateTime; ValueB : DateTime ) : Integer; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns one of the following values :
 
ValueA < ValueB Gives <0
ValueA = ValueB Gives  0
ValueA > ValueB Gives >0

 
The exact value 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;
  result               : Integer;

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

  result := System.DateTime.Compare(dateTime1, dateTime2);
  Console.WriteLine('dateTime1 = {0:F}', dateTime1);
  Console.WriteLine('dateTime2 = {0:F}', dateTime2);
  Console.WriteLine('dateTime1 vs dateTime2 = {0}', result.ToString);

  Console.ReadLine;
end.
   dateTime1 = 20 June 2004 00:00:00
   dateTime2 = 21 June 2004 00:00:00
   dateTime1 vs dateTime2 = -1
Showing the invisible effect of small tick differences
program Project1;
{$APPTYPE CONSOLE}

var
  dateTime1, dateTime2 : DateTime;
  result               : Integer;

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;

  result := System.DateTime.Compare(dateTime1, dateTime2);
  Console.WriteLine('dateTime1 = {0:F}', dateTime1);
  Console.WriteLine('dateTime2 = {0:F}', dateTime2);
  Console.WriteLine('dateTime1 vs dateTime2 = {0}', result.ToString);

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

  dateTime1 := dateTime1.AddTicks(1);

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

  Console.ReadLine;
end.
   Starting with identical date/time values
  
   dateTime1 = 20 June 2004 00:00:00
   dateTime2 = 20 June 2004 00:00:00
   dateTime1 vs dateTime2 = 0
  
   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 vs dateTime2 = 1
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author