Geo
Geo

Reputation: 3200

Time Format - Coldfusion 9

I am trying to create a time stamp in coldfusion that would include milliseconds.

My issue is that I cannot find a code anywhere that would allow me to keep the format consistent by controlling leading zeros.

This is my format:

<cfoutput> 
<cfset todayDate = #Now()#> 
<ul> 
    <li>#TimeFormat(todayDate, "HH:mm:ssl")# </li>
</ul> 
</cfoutput>  

I just need something like "HH:mm:ssll" or some other method that will ensure that I would have a 9 digit timestamp at all times.

Upvotes: 5

Views: 10267

Answers (4)

jackRoark
jackRoark

Reputation: 71

You almost had it when you said:

I just need something like "HH:mm:ssll"...

Just add a third "l" to have the correct number of placeholders:

<cfoutput> 
    <cfset todayDate = #Now()#> 
        <ul> 
            <li>#TimeFormat(todayDate, "HH:mm:sslll")# </li>
        </ul> 
</cfoutput>  

Upvotes: 2

Mike Causer
Mike Causer

Reputation: 8314

Use java SimpleDateFormat!

<cfscript>
   createObject('java','java.text.SimpleDateFormat').init('yyyy-MM-dd HH:mm:ss.SSS Z').format(now());
</cfscript>

Produces 2010-07-19 11:40:14.051 EST

<cfscript>
   createObject('java','java.text.SimpleDateFormat').init('HH:mm:ss.SSS').format(now());
</cfscript>

Produces 09:45:12.009 - with leading zeros

Upvotes: 8

Geo
Geo

Reputation: 3200

    <cfscript>
    function getUniqueID() {
        rightNow = now();
return (dateformat(rightNow,'yyyymmdd') & timeformat(rightNow,"HHmmss") &NumberFormat(TimeFormat(rightNow, "l"),"000") & RandRange(10000, 99999));

    }
    </cfscript>

<cfdump var="#getUniqueID()#">

Just sharing my code in case someone needs to create a unique timestamp for whatever purpose.

Credits to Henry for the NumberFormat part of the code

Upvotes: 0

Henry
Henry

Reputation: 32905

Milliseconds with leading zeros?

<li>
  #TimeFormat(todayDate, "HH:mm:ss")##NumberFormat(TimeFormat(todayDate, "l"),"000")#
</li>

FYI, l has maximum of 3 digits. So I'm not sure about your 9-digits limit.

Upvotes: 8

Related Questions