Ben Forta had posted something about subtracting one date from another and it got me thinking. If you follow my blog, you know that I am a big fan of performing straight up date math (leveraging the fractional format of a date) rather than using built in ColdFusion methods such as DateAdd() and CreateDate() WHEN I CAN (which is not all the time of course). So anyway, it reminded me of the time I compared CreateDate() to DateFormat() in terms of speed of removing time. I was pretty sure that date math would be faster than things like CreateDate() but I wanted to test for confirmation.
I set up two test. One that removes the time from a date and one that subtracts a day from a date.
Often times in an application I will want to remove the time from a date before I either use it in a database query or a loop. This compares the Int() method which rounds DOWN on a date to the CreateDate() method which will create a time-less date object:
Launch code in new window » Download code as text file »
In these test, just as I suspected, the math method was faster. For 10,000 iterations, the math method ran at best 3 times faster than CreateDate() and at worst was on par with CreateDate(). But if you think about it, it just makes sense - CreateDate() uses 4 method calls where as the math method uses only two method calls.
Often times in an application, I will need to change a given date by adding or subtracting days from a it, such as finding the date 7 days from the given date. This test was to compare the math method to the DateAdd() method:
Launch code in new window » Download code as text file »
This test proved that date math is faster than DateAdd(). However, the results where not as big. Subtracting a number from a date outperformed DateAdd() only slightly to moderately, not drastically.
I have talked about this before, but I will touch on it once again as it is such an important concept to understand. Date/Time objects can be considered to represent the number of days that have passed since the zero day, where the integer part represents the whole days and the decimal part represents the time. The zero day differs from system to system (I think), but for ColdFusion, the zero day is Saturday, December 30, 1899. Taking that into account, the date "100" is merely 100 days later than the zero date. That is why the following two lines output the same thing:
Launch code in new window » Download code as text file »
Now, that is not to say that you cannot go back in time even farther. The only difference is that the fractional representation of the number becomes negative. That is why:
Launch code in new window » Download code as text file »
... will give you Wednesday, December 20, 1899. This is 10 days before ColdFusion's zero date.
There you have it; date/time objects are numeric. That is why ColdFusion has a built in method to determine if your number is a valid date: IsNumericDate(). So, feel free start adding numbers and dates together to get some really fast results. Just remember, one day is 1.
Download Code Snippet ZIP File
Comments (0) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
There are no comments posted for this web log entry.