View comments | RSS feed

DateCompare

Description

Performs a full date/time comparison of two dates.

Returns

Category

Date and time functions

Function syntax

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

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. 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, you must enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.

Example

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

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


TomGru said on Nov 11, 2005 at 2:26 AM :
Ther is a Bug in all Date functions like DateDiff, DateAdd DateCompare etc.
If you have the German Version of CF it can happen that you submit e.g a Date like 03.11.2005 this date is interpreted as mm.dd.yyyy and therefore as 11.03.2005 !! CF changes the Dateformat as log as it is possible 13.11.2005 is interpreted correctly with dd.mm.yyyy

you can avoid this if you create an new date out of the formdata:

<cfparam name="datumswert" default=""><!--- Format dd.mm.yyyy --->
<cfset NewDate=CreateDate(mid(datumswert,7,4), mid(datumswert,4,2), mid(datumswert,1,2))>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000438.htm