Generate a 14-digits unique number from the current date/time

This function generates a 14-digits unique number that you may need when you assign unique IDs/numbers for database records.

hint="This function generates a 14-digit unique number from the current date and time">
<cfparam name="result" default="0">
get the year, month, day, hour, minute, and second from the current date/time and store them in variables
<cfset vYear=year(now())>
<cfset vMonth=month(now())>
<cfset vDay=day(now())>
<cfset vHour=hour(now())>
<cfset vMinute=minute(now())>
<cfset vSecond=second(now())>
check the length of the month, day, hour, minute, and second. if the length is less than 0 then store "00" into the variables, if the
length is 1 then append "0" to the beginning of the variable value. this is to ensure that the generated number is 14-digits.
<cfif len(vMonth) EQ 0>
<cfset vMonth="00">
<cfelseif len(vMonth) EQ 1>
<cfset vMonth="0" & vMonth>
<cfif len(vDay) EQ 0>
<cfset vDay="00">
<cfelseif len(vDay) EQ 1>
<cfset vDay="0" & vDay>
<cfif len(vHour) EQ 0>
<cfset vHour="00">
<cfelseif len(vHour) EQ 1>
<cfset vHour="0" & vHour>
<cfif len(vMinute) EQ 0>
<cfset vMinute="00">
<cfelseif len(vMinute) EQ 1>
<cfset vMinute="0" & vMinute>
<cfif len(vSecond) EQ 0>
<cfset vSecond="00">
<cfelseif len(vSecond) EQ 1>
<cfset vSecond="0" & vSecond>
concatenate the variables in the format "YYYYMMDDHHMMSS" and return the result.
<cfset result = vYear & vMonth & vDay & vHour & vMinute & vSecond>
<cfreturn result>

About This Tutorial
Author: Ikramy Ghidan
Skill Level: Intermediate 
Platforms Tested: CF5,CFMX,CFMX7
Total Views: 110,581
Submission Date: May 31, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 2
Discuss This Tutorial
  • CreateUUId() is a CF function that creates a unique 35 charactor ID. I'm curious why would you need to write 35+ rows of code to do what this function does in one line?

  • Hi, To solve the busy website issue, you can use the CFLock tag. Good luck

  • Hi there, I've been using a very similar approach to this - nice to see it as a component. However, this wouldn't work for a busy website as two (or more) people could click submit buttom at the same time, thus producing the same id.


Sponsored By...
Healing Touch Massage - Dripping Springs, Texas - Deep Tissue Massage and Swedish Massage Services just $39 for a 50 minute massage!