DateTime.Now.Compare(To)

.Compare(To) compares the TimeSpan between two different DateTime objects. The DateTime object in the "To" parameter is subtracted from the DateTime object calling the Compare(To) method. As an example: Date1 - Date2 would be expressed as Date1.Compare(Date2).

Applies To

Properties and Methods

Properties

  • .Days: (Int32) Returns the number of days from Math.Truncate(TotalDays).
  • .Hours: (Int32) Returns the number of hours from Math.Truncate(TotalHours - (Days * 24)).
  • .Milliseconds: (Int32) Returns the number of milliseconds from Math.Truncate(TotalMilliseconds - ((Days * 24 * 60 * 60 * 1000) + (Hours * 60 * 60 * 1000) + (Minutes * 60 * 1000) + (Seconds * 1000))).
  • .Minutes: (Int32) Returns the number of minutes from Math.Truncate(TotalMinutes - ((Days * 24 * 60) + (Hours * 60))).
  • .Seconds: (Int32) Returns the number of seconds from Math.Truncate(TotalSeconds - ((Days * 24 * 60 * 60) + (Hours * 60 * 60) + (Minutes * 60))).
  • .Ticks: (Int64) Returns the total number of ticks between the two DateTime objects.
  • .TotalDays: (Double) Returns the total number of days between the two DateTime objects. Partial days are expressed as a decimal.
  • .TotalHours: (Double) Returns the total number of hours between the two DateTime objects. Partial hours are expressed as a decimal.
  • .TotalMilliseconds: (Double) Returns the total number of milliseconds between the two DateTime objects. Partial milliseconds are expressed as a decimal.
  • .TotalMinutes: (Double) Returns the total number of minutes between the two DateTime objects. Partial minutes are expressed as a decimal.
  • .TotalSeconds: (Double) Returns the total number of seconds between the two DateTime objects. Partial seconds are expressed as a decimal.

Available

The .Compare(To) method is available in:

  • 16.05.087
  • 16.06.035
  • 16.07.008
  • 17.00.005
  • All newer builds

Type

TimeSpan

Syntax

DateTime.Now.Compare(To);

Parameters

Parameter

Required

Description

To

Yes

A DateTime object. If left null, then .Compare(To) returns null

Non-DateTime objects generate a scripting error.

.Property

Yes

One of the listed properties from above is required to return a result

Example

var ldTest1 = new DateTime(2021, 10, 26, 10, 28, 24); //--10/26/2021 10:28:24

var ldTest2 = new DateTime(2019, 9, 1, 12, 30, 45); //--9/1/2019 12:30:45

var loInterval = ldTest1.Compare(ldTest2);

var lcOutput = 'Comparing ldTest1 (' + Convert.ToString(ldTest1) + ') to ldTest2 (' + Convert.ToString(ldTest2) + '):\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Days: ' + loInterval.Days + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).TotalDays: ' + loInterval.TotalDays + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Hours: ' + loInterval.Hours + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).TotalHours: ' + loInterval.TotalHours + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Minutes: ' + loInterval.Minutes + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).TotalMinutes: ' + loInterval.TotalMinutes + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Seconds: ' + loInterval.Seconds + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).TotalSeconds: ' + loInterval.TotalSeconds + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Milliseconds: ' + loInterval.Milliseconds + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).TotalMilliseconds: ' + loInterval.TotalMilliseconds + '\r\n';

lcOutput += 'ldTest1.Compare(ldTest2).Ticks: ' + loInterval.Ticks;

Event.Form.MessageBox(lcOutput);

 

/* Expected Prompt response:

Comparing ldTest1 (10/26/2021 10:28:24) to ldTest2 (09/01/2019 12:30:45):

ldTest1.Compare(ldTest2).Days: 785

ldTest1.Compare(ldTest2).TotalDays: 785.9150347222222

ldTest1.Compare(ldTest2).Hours: 21

ldTest1.Compare(ldTest2).TotalHours: 18861.96083333333

ldTest1.Compare(ldTest2).Minutes: 57

ldTest1.Compare(ldTest2).TotalMinutes: 1131717.6500000001

ldTest1.Compare(ldTest2).Seconds: 39

ldTest1.Compare(ldTest2).TotalSeconds: 67903059

ldTest1.Compare(ldTest2).Milliseconds: 0

ldTest1.Compare(ldTest2).TotalMilliseconds: 67903059000

ldTest1.Compare(ldTest2).Ticks: 679030590000000

*/