DateCompare(date1, date2 [, datePart])

Description

Performs a full date/time comparison of two dates. Returns:

Category

Date and time functions

See also

CreateDateTime, DatePart

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. The precision of the comparison. The parameter can have any of the following values:
  • s Precise to the second
  • 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
By default, precision is to the second

Usage

When passing a date/time value as a string, enclose it in quotes. Otherwise, it is interpreted as a number representation of a date/time object.

Year values 0 - 29 are interpreted as 21st century dates. Year values 30 - 99 are interpreted as 20th century dates.

Example

<!--- 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>