October 5, 2007

createODBCDate() returns 'time' as well?

Recently I was just comparing 2 dates (one from a query and the other was now()) using the createODBCDate() and dateCompare() functions.

I was using the createODBCDate() function because I only wanted the ‘date’ part of a date object, not the ‘time’. More than likely I should have been using dateFormat(), but that’s another story :)

I had the following:

<cfif dateCompare(createODBCDate(q.DateTimeCreated), createODBCDate(now())) EQ 0>

The problem was I kept getting -1 returned, meaning that ‘q.DateTimeCreated’ was less than ‘now()’. As ‘q.DateTimeCreated’ was technically created before ‘now()’ (same day but an earlier ‘time’) it is actually less than, but I only wanted to compare the date…not the time. When I dumped both date objects I got:

{d '2007-10-05'}
{d '2007-10-05'}

Looks the same to me…

I think that those date objects actually have the ‘time’ in them as well, they’re just not being displayed on cfdump or cfoutput. This seems strange especially considering there is a createODBCDateTime() function as well.

My fix was to do what I probably should have done in the first place; use dateFormat()

<cfif dateCompare(dateFormat(q.DateTimeCreated), dateFormat(now())) EQ 0>

That did the trick!

Now the only thing bothering me is whether the createODBCDate() function actually does return the ‘time’ as well…or was something else the culprit?

© Michael Sharman 2017