CreateDate() Much Faster Than DateFormat() For Date-Only Creation

Posted June 7, 2006 at 8:22 AM

Tags: ColdFusion, SQL

There are times when I need a date/time stamp that only has a date part and a zero'd out time part. Take for example, needing to get all events on a given day. I can't use a simple =, <, or > since the time part will not compare easily or work nicely with DateAdd() execution. But don't get hung up on the example, just imagine that sometimes you want a date-only date/time object.

To do this, I usually do a CreateDate() on the date/time stamp to get a date-only date object:

 Launch code in new window » Download code as text file »

  • CreateDate( [YEAR], [MONTH], [DAY] )

I thought maybe it would be faster to do a DateFormat() call since this requires less method calls (superficially - does not have to call Year(), Month(), Day() methods). Plus, DateFormat() looks a bit more streamlined.

It turns out that DateFormat(), for this purpose, is MUCH slower than CreateDate(). In general, my testing (below) found CreateDate() to be about 8 times faster than DateFormat().

 Launch code in new window » Download code as text file »

  • <!--- Set up test for CreateDate(). --->
  • <cftimer label="Using CreateDate()" type="outline">
  •  
  • <!--- Loop over enough times to see a non-zero time. --->
  • <cfloop index="intI" from="1" to="1000" step="1">
  •  
  • <!--- Get date/time stamp. --->
  • <cfset dtNow = Now() />
  •  
  • <!--- Get the "date-only" part of the date/time stamp. --->
  • <cfset dtNowDate = CreateDate( Year( dtNow ), Month( dtNow ), Day( dtNow ) ) />
  •  
  • <!--- Do some computation on the date. --->
  • <cfset dtComputed = DateAdd( "d", RandRange( 1, 5 ), dtNowDate ) />
  •  
  • <!--- For screen output. --->
  • .
  •  
  • </cfloop>
  •  
  • </cftimer>
  •  
  • <!--- Set up test for DateFormat(). --->
  • <cftimer label="Using DateFormat()" type="outline">
  •  
  • <!--- Loop over enough times to see a non-zero time. --->
  • <cfloop index="intI" from="1" to="1000" step="1">
  •  
  • <!--- Get the "date-only" part of the date/time stamp. --->
  • <cfset dtNowDate = DateFormat( Now(), "mm/dd/yyyy" ) />
  •  
  • <!--- Do some computation on the date. --->
  • <cfset dtComputed = DateAdd( "d", RandRange( 1, 5 ), dtNowDate ) />
  •  
  • <!--- For screen output. --->
  • .
  •  
  • </cfloop>
  •  
  • </cftimer>

However, keep in mind that this is for extreme bulk date creation. For a single instance on a page, the difference would be inconsequential. So the question is, which is "better"? I guess it depends. If this is going to be in an encapsulated area, then by all means, use the faster, more efficient CreateDate() method. But, if you are going to use it once or twice on a page in a CFQueryParam or something, I say go with which ever one seems easier to read and maintain.

Download Code Snippet ZIP File

Comments (0)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting