# DateCompare

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 and DatePart.

### Syntax

    DateCompare(date1, date2 [, datePart])

##### date1

Date/time object in the period from 100 AD to 9999 AD.

##### date2

Date/time object in the period from 100 AD to 9999 AD.

##### datePart

Optional. The precision of the comparison. This 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, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object, returning undesired results.

Years from 0 to 29 are interpreted as 21st century values. Years 30 to 99 are interpreted as 20th century values.

### Examples

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