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

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »