Performs a full date/time comparison of two dates. Returns -1 if date1 is less than date2; returns 0 if date1 is equal to date2; returns 1 if date1 is greater than date2. See the description of datePart for information on specifying the precision of the comparison.
See also CreateDateTime.
DateCompare(date1, date2 [, datePart])
Date/time object in the period from 100 AD to 9999 AD.
Date/time object in the period from 100 AD to 9999 AD.
Optional. The precision of the comparison. This parameter can have any of the following values:
By default, precision is to the second.
When passing a date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object, returning undesired results.
<!--- This example shows the use of datecompare --->
<HTML>
<HEAD>
<TITLE>
DateCompare Example
</TITLE>
</HEAD>
<BASEFONT FACE="Arial, Helvetica" SIZE=2>
<H3>DateCompare Example</H3>
<P>The datecompare function compares two date/time values.
<CFIF IsDefined("FORM.date1")>
<CFIF IsDate(FORM.date1) and IsDate(FORM.date2)>
<CFSET comparison=DateCompare(FORM.date1, FORM.date2,
FORM.precision)>
<!--- switch on the variable to give various responses --->
<CFSWITCH EXPRESSION="#comparison#">
<CFCASE value="-1">
<H3><CFOUTPUT>#DateFormat(FORM.date1)#
#TimeFormat(FORM.date1)#</CFOUTPUT> (Date 1) is
earlier than <CFOUTPUT>#DateFormat(FORM.date2)#
#TimeFormat(FORM.date2)#</CFOUTPUT> (Date 2)</H3>
<I>The dates are not equal</I>
</CFCASE>
<CFCASE value="0">
<H3><CFOUTPUT>#DateFormat(FORM.date1)#
#TimeFormat(FORM.date1)#</CFOUTPUT> (Date 1) is equal
to <CFOUTPUT>#DateFormat(FORM.date2)#
#TimeFormat(FORM.date2)#</CFOUTPUT> (Date 2)</H3>
<I>The dates are equal!</I>
</CFCASE>
<CFCASE value="1">
<H3><CFOUTPUT>#DateFormat(FORM.date1)#
#TimeFormat(FORM.date1)#</CFOUTPUT> (Date 1) is later
than <CFOUTPUT>#DateFormat(FORM.date2)#
#TimeFormat(FORM.date2)#</CFOUTPUT> (Date 2)</H3>
<I>The dates are not equal</I>
</CFCASE>
<CFDEFAULTCASE>
<H3>This is the default case</H3>
</CFDEFAULTCASE>
</CFSWITCH>
<CFELSE>
<H3>Please enter two valid date values</H3>
</CFIF>
</CFIF>
<FORM ACTION="datecompare.cfm" METHOD="POST">
<HR size="2" color="#0000A0">
<P>Date 1
<BR><INPUT TYPE="Text" NAME="date1" VALUE="<CFOUTPUT>#DateFormat(Now())#
#TimeFormat(Now())#
</CFOUTPUT>">
<P>Date 2
<BR><INPUT TYPE="Text" NAME="date2" VALUE="<CFOUTPUT>#DateFormat(Now())#
#TimeFormat(Now())#
</CFOUTPUT>">
<P>Specify precision to the:
<BR><SELECT NAME="precision">
<OPTION VALUE="s">
Second
</OPTION>
<OPTION VALUE="n">
Minute
</OPTION>
<OPTION VALUE="h">
Hour
</OPTION>
<OPTION VALUE="d">
Day
</OPTION>
<OPTION VALUE="m">
Month
</OPTION>
<OPTION VALUE="yyyy">
Year
</OPTION>
</SELECT>
<P><INPUT TYPE="Submit" VALUE="Compare these dates" NAME="">
<INPUT TYPE="RESET">
</FORM>
</BODY>
</HTML>