Search code examples
datetimecoldfusioncoldfusion-10coldfusion-11coldfusion-2016

Adding random time to DateTime value


I would like to add a random time between 1-30 minutes to a date, but ColdFusion doesn't seem to like it. It adds the same amount of minutes to the Now() value no matter how many times I run the following code. I can't figure out why.

<cfset DateFuture = DateTimeFormat(DateAdd('n', RandRange(1, 30), Now()), 'yyyy-mm-dd HH:mm:ss.l')/>  
<cfoutput>#DateFuture#</cfoutput>

I need the yyyy-mm-dd HH:mm:ss.l DateTimeFormat because this what my datetime values in SQL Server look like which is where I plan to insert my future date.

If I run the above code I keep getting this output:

2017-11-25 22:11:24 and then suddenly it will change to 2017-11-25 21:11:16 which is taking away a whole hour when I only want to add to the time!

It makes no sense why its behaving like this. I am in the UK but am using international date format in the default format of SQL Server which is like above.

UPDATE: Its a typo mistake! The DateTimeFormat should be yyyy-mm-dd HH:nn:ss.l. 'nn' is minutes, not 'mm'. D'oh!


Solution

  • The whole problem was down to a typo. It should be HH:nn:ss. Notice the nn and not mm for minutes.