Notes from the field on ColdFusion (or related) technical issues.

Tuesday, June 01, 2010

CreateODBCDate Retains Time Data

Had an interesting "WTF moment" over the weekend. I tried to prevent a batch job from running on Memorial Day by using the following code:


<cfif dateCompare(createOBDCDate(now()), "May 31, 2010") is 0>
<cfabort />
</cfif>


But this failed to stop the job from running..? So I investigated further:


<cfoutput>
<cfset today = createOBDCDate(now()) />
#today#<br />
#createODBCDate("May 31, 2010")#<br />
#datecompare(today, "May 31, 2010")#
</cfoutput>


Which produced:


{d '2010-05-31'}
{d '2010-05-31'}
1


Okay... so those dates are different. WTF?


<cfoutput>
<cfset today = createOBDCDate(now()) />
#today#<br />
#timeformat(today)#<br />
#createODBCDate("May 31, 2010")#<br />
#datecompare(today, createODBCDate("May 31, 2010"))#
</cfoutput>


This returned:


{d '2010-05-31'}
12:11 PM
{d '2010-05-31'}
1


So... while CreateODBCDate() returns a formatted SQL date-only string, it invisibly retains the time information. Wow.

I wound up doing it the hard way:


<cfset today = createDate(year(now()), month(now()), day(now()))>


<sigh />