I’m having an issue with the DateCompare function on CF8. I’m using it to compare Arrival and Departure Dates on a form. When the form is submitted I do the following:
<cfset dep = DateCompare(Form.Departure, Form.Arrival)>
<cfswitch expression="#dep#">
<cfcase value="-1">Stop! <cfoutput>#dep#</cfoutput>
redirect to error page as can't depart before you arrive
</cfcase>
<cfcase value="0, 1">
<p>OK!!!!! process</p>
</cfcase>
</cfswitch>
9 times out of 10 this works fine. However when it comes to dates that span the end/beginning of months it returns incorrect information. For example Arrival - 31/03/2011 Departure 01/04/2011 returns a -1 to say that departure is before arrival?! This only starts working again when Arrival is 31/03/2011 and Departure is 04/04/2011.
I already have a javascript function that checks this, but I need to have something for people without javascript enabled. Can anyone think of another way of doing this? Or is this a known issue?
Any help gratefully received!
<cfset form.arrival = "2011/03/14" />
<cfset form.departure = "2011/03/15" />
<cfset datArrival = createDate( listGetAt( form.arrival, 1, "/" ), listGetAt( form.arrival, 2, "/" ), listGetAt( form.arrival, 3, "/" ) ) />
<cfset datDeparture = createDate( listGetAt( form.departure, 1, "/" ), listGetAt( form.departure, 2, "/" ), listGetAt( form.departure, 3, "/" ) ) />
<cfdump var="1 = arrival is earlier than departure. 0 = same. -1 = arrival is later than departure : #dateCompare( datArrival, datDeparture )#" />
In DateCompare, you need to compare 2 DATE OBJECTS. Since you never turned the dates into a date object, they are seen as a STRING. And you’re trying to do a STRING compare with the DATECOMPARE function.
The above code takes what was submitted and forces them into a date object; you should get more reliable results with that.
FYI too, you need to ensure that the data is provided in the proper format. Personally, I use “mm-dd-yyyy”. I know Europe and other locations use formats like “dd-mm-yyyy” but make sure you change the ListGetAt to ensure the createDate works properly (CreateDate expects year, then month, then day)
Thanks Aaron! I knew there’d be something obvious I wasn’t doing.