Over the weekend, I came up against the ColdFusion task of selecting a random date between two given dates. In the past, I have created a ColdFusion user defined function, RandDateRange( dtFrom, dtTo ), and I thought this would be a perfect time to apply it. After reviewing that one though, I see that it not only randomizes the date but also the time by selecting a random mid point in seconds between the two dates.
Well, what happens when I don't want to include a random time? I suppose that I could just Fix() the value that gets returned. But what fun would that be? I had some time on my hands, so I figured I would update the RandDateRange() function to handle date-only values as well.
This modified ColdFusion UDF takes two dates and a flag for randomization of time. If both dates are integer values (they have no time parts) then the function randomizes the date only unless the time randomization flag is sent as true. If either of the passed-in dates includes a time part, then time is also randomized. In addition, this update also formats the returned values. The previous version of the UDF passed back numeric values for the date. This works fine, if you know what's going on, but this returned value will fail IsDate() calls. And, unless you know to use IsNumericDate() calls, you might be lost.
Launch code in new window » Download code as text file »
Notice that we are calling DateFormat() for the date-only randomization and DateFormat() and TimeFormat() for the date/time randomization. This will result in a true date value that will return TRUE when passed to IsDate().
In this test, we are going to select JUST the date value between two given dates:
Launch code in new window » Download code as text file »
This gives us the output:
19-May-07
20-May-07
16-May-07
01-May-07
10-May-07
Now, let's run that same thing, except this time, let's pass through the flag for additional time randomization.
Launch code in new window » Download code as text file »
This gives us the output:
15-May-07 05:36 PM
10-May-07 04:41 AM
14-May-07 01:34 PM
18-May-07 06:23 AM
07-May-07 10:03 PM
Works like a charm, and notice that the values getting returned don't even have to be formatted - they are true dates (in string format).
Now, just one final test to demonstrate that when one of the dates has a time portion, the randomized date also has a time portion:
Launch code in new window » Download code as text file »
Running the above we get:
01-May-07 12:32 PM
01-May-07 10:48 AM
01-May-07 03:13 PM
01-May-07 01:34 PM
01-May-07 11:41 AM
I think this is a good update to the previous RandDateRange() ColdFusion user defined function.
Download Code Snippet ZIP File
Comments (6) | Post Comment | Ask Ben | Permalink | Print Page
Getting The Decimal Part Of A Number In ColdFusion
Two New ColdFusion eCards Available (Including A User Submission!) (Saucy)
I enjoy this code. But for what reasons I will need this function? I don't find practical implementation
Posted by Builder on May 7, 2007 at 9:27 AM
Yeah, true, not a ton of practical uses. To be honest, I am not sure why I even tried to do this. I know I had a task in mind, but cannot recall it.
Looks like someone is having a case of the Mondays :)
Posted by Ben Nadel on May 7, 2007 at 9:46 AM
You have been messing with dates a lot this weekend, huh? Just a hunch is all.
Posted by Cozmo on May 7, 2007 at 10:45 AM
Yeah, when I get stuck on a problem, or I get an idea that I want to work on, I try to explore different aspects of it - see what I missed or I don't understand. I even try to tackle old problems sometimes to see if the time since they were first solved has shed light on better ways of doing things.
Plus, sometimes there's nothing good on the 2 TV channels that I get via my antenna :)
Posted by Ben Nadel on May 7, 2007 at 10:50 AM
You should consider using including a calendar table in your database- it makes life a whole lot easier! Just creating a simple table containing the date, whether is is a weekday, the year, quarter, month, day, day of week, month name, dayname, week number, holiday, etc. makes picking random days or doing certain types of reporting a snap. Plus you are putting the work on the db server, not on the application server.... if you google "TSQL calendar table" you should find some examples.
Posted by Bob Clingan on May 7, 2007 at 2:26 PM
@Bob,
What you said got me thinking... not necessarily about this post, but about calendars in general. I think it would be awesome to store additional "info" columns about the date. Things like day of the week (1-7) or like "is_last_day", or "is_first_monday"... things like that which are super easy to calculate on input to the database, but less easy/efficient to calculate whilst querying the database.
Posted by Ben Nadel on May 7, 2007 at 2:33 PM