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 »
10,743 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »