Date string comparison in Coldfusion
I discovered the other day an interesting tid-bit in cf 8 when comparing two date strings that were set as the following:
<cfset date1 = '5/19/2010' /> <cfset date2 = '19/5/2010' /> <cfif date1 eq date2>the same<cfelse>different</cfif>
When doing so the strings are considered equal. I gather this is because it translates both strings as dates automatically but one is in d/m/yyyy format and the other is m/d/yyyyy format. This was troublesome for me since I was comparing two dates that come from a possible user input, entering them into a text field of a form as an alternative to using the date picker.
DateCompare() results the same. In order to solve my issue without creating a brand new date using createDate(), I was able to use the compare() function as in the following:
<cfif compare(date1,date2) eq 0>the same<cfelse>different</cfif>
Strange thing about cf but not really a bug.
| Print article | This entry was posted by admin on May 16, 2010 at 6:13 pm, and is filed under Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 3 months ago
BTW, this seems to be the case in cf 9 as well