<cffunction name="RandDateRange" access="public" returntype="date" output="false" hint="Returns a random date between the two passed in dates. If either of the two dates has a time value, the resultant time will also be randomized."> <!--- Define arguments. ---> <cfargument name="DateFrom" type="date" required="true" hint="The min date of the date range." /> <cfargument name="DateTo" type="date" required="true" hint="The max date of the date range." /> <cfargument name="IncludeRandomTime" type="boolean" required="false" default="false" hint="Will include the time in the date randomization even if neither passed-in dates have a time." /> <!--- Define the local scope. ---> <cfset var LOCAL = StructNew() /> <!--- Check to see if we are going to randomize time. If either of the passed in dates has a non-12 AM time, then we are going to include a random time. We will know if a date is include if the either date does not equal its Fixed value. ---> <cfif ( (ARGUMENTS.DateFrom NEQ Fix( ARGUMENTS.DateFrom )) OR (ARGUMENTS.DateTo NEQ Fix( ARGUMENTS.DateTo )) OR ARGUMENTS.IncludeRandomTime )> <!--- Get the difference in seconds between the two given dates. Once we have this value, we can pick a random mid point in this span of seconds. ---> <cfset LOCAL.DiffSeconds = DateDiff( "s", ARGUMENTS.DateFrom, ARGUMENTS.DateTo ) /> <!--- Now that we know the second difference between the two dates, we can easily use RandRange() to get a random second span that we will add to the start date to give us a random mid date. ---> <cfset LOCAL.Date = ( ARGUMENTS.DateFrom + CreateTimeSpan( 0, <!--- Days. ---> 0, <!--- Hours. ---> 0, <!--- Minutes ---> <!--- Now, let's pick the random number of seconds for this added time span. ---> RandRange( 0, LOCAL.DiffSeconds ) ) ) /> <!--- Now that we have the randome date/time value, we need to format it using both the date and the time. We cannot just send back the DateFormat() (as in the other case) since that would strip out the time. ---> <cfreturn DateFormat( LOCAL.Date ) & " " & TimeFormat( LOCAL.Date ) /> <cfelse> <!--- We are not going to include a random time. Therefore, we can just get a random integer to represent the date (no time). Since date/time values can be represented as float values, we can just use RandRange() to get a random integer which we will then convert back to a date/time value. DateFormat() will convert it back to a date value with zero time. ---> <cfreturn DateFormat( RandRange( ARGUMENTS.DateFrom, ARGUMENTS.DateTo ) ) /> </cfif> </cffunction>