User Guide Cancel

DateCompare

 

Description

Performs a full date/time comparison of two dates.

Returns

  • -1, if date1 is earlier than date2
  • 0, if date1 is equal to date2
  • 1, if date1 is later than date2

Category

Date and time functions

Function syntax

DateCompare(date1, date2 [, datePart])
DateCompare(date1, date2 [, datePart])
DateCompare(date1, date2 [, datePart])

See also

CreateDateTimeDatePart

Parameters

Parameter

Description

date1

Date/time object, in the range 100 AD-9999 AD.

date2

Date/time object, in the range 100 AD-9999 AD.

datePart

Optional. String. Precision of the comparison.

  • s-Precise to the second (default)
  • n-Precise to the minute
  • h-Precise to the hour
  • d-Precise to the day
  • m-Precise to the month
  • yyyy-Precise to the year

Usage

When passing a date/time object as a string, enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.

Example

<cfscript>
Date1 = "{ts '2018-11-15 12:13:50'}";
Date2 = "{ts '2018-10-15 11:13:50'}";
Date3 = "{ts '2018-11-15 12:13:50'}";
Date4 = "{ts '2019-11-15 12:13:50'}";
Compare= DateCompare(Date1,Date2)
Compare1= DateCompare(Date1,Date3)
Compare2= DateCompare(Date1,Date4)
writeOutput("date comparison is : " & Compare & "<br/>")
writeOutput("date comparison is : " & Compare1 & "<br/>")
writeOutput("date comparison is : " & Compare2 & "<br/>")
</cfscript>
<cfscript> Date1 = "{ts '2018-11-15 12:13:50'}"; Date2 = "{ts '2018-10-15 11:13:50'}"; Date3 = "{ts '2018-11-15 12:13:50'}"; Date4 = "{ts '2019-11-15 12:13:50'}"; Compare= DateCompare(Date1,Date2) Compare1= DateCompare(Date1,Date3) Compare2= DateCompare(Date1,Date4) writeOutput("date comparison is : " & Compare & "<br/>") writeOutput("date comparison is : " & Compare1 & "<br/>") writeOutput("date comparison is : " & Compare2 & "<br/>") </cfscript>
<cfscript>
    Date1 = "{ts '2018-11-15 12:13:50'}";
    Date2 = "{ts '2018-10-15 11:13:50'}";
    Date3 = "{ts '2018-11-15 12:13:50'}";
    Date4 = "{ts '2019-11-15 12:13:50'}";
    Compare= DateCompare(Date1,Date2)
    Compare1= DateCompare(Date1,Date3)
    Compare2= DateCompare(Date1,Date4)
    writeOutput("date comparison is : " & Compare & "<br/>")
    writeOutput("date comparison is : " & Compare1 & "<br/>")
    writeOutput("date comparison is : " & Compare2 & "<br/>")
</cfscript>

Output

date comparison is : 1
date comparison is : 0
date comparison is : -1

Get help faster and easier

New user?