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

Posted June 7, 2006 at 8:22 AM by Ben Nadel

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:

  • 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().

  • <!--- 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.



Reader Comments

Nov 10, 2010 at 1:00 PM // reply »
1 Comments

Thanks a lot for this Ben.
Just one of the thousand other posts you've help me with.

Thanks
Arshad


Nov 10, 2010 at 7:52 PM // reply »
11,243 Comments

@Arshad,

Ha ha, thanks my man :)


Mar 29, 2011 at 4:49 PM // reply »
1 Comments

You shouldn't really use dateformat() unless you are outputting the data to the screen. Why? If you ever want to internationalise your code you'll run into trouble. CFs date formatting functions only work reliably with American date masks (month then date) unless you use CF date objects to store the dates.

You should use CreateDate() to make a date object and use that at all times for internal date functions (like adding etc), and only use dateFormat() at the last moment for display.

Here's an example of what happens just using dateformat:

  • <!--- Here I am in the UK - this date means 2nd April 2011 --->
  • <cfset myDateString = "02/04/11">
  •  
  • <cfloop index="i" from="1" to="5">
  • <cfset myDateString = DateFormat( myDateString, "dd/mm/yyyy" ) />
  • <cfoutput>#myDateString#</cfoutput>
  • </cfloop>

Run that and if you set an initial UK date between the 1st and 12th of a month you'll get:

  • 04/02/2011
  • 02/04/2011
  • 04/02/2011
  • 02/04/2011
  • 04/02/2011


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools