Copy link to clipboard
Copied
Hi all, I need to create a timestamp of difference in milliseconds between the date and time now and 00:00:00 01/01/1970.
Thanks, Paul
By @Outside5 Ltd
There is a neat answer: getTickCount().
That's it, an in-built function in ColdFusion whose return-value is an exact answer to the original question of this thread.
Test:
<cfset tickCount=gettickcount()>
<cfset secondsDiff=tickCount/1000>
As a check, the start time must be equivalent to the Unix Epoch (January 1, 1970, 00:00 GMT). <br>
<
...
Anyway, I think the most important factor to consider when you read this thread is time itself. In January 2007, when the original question was asked, ColdFusion was still on version 7, running on Java 6.
Seven releases later, ColdFusion is now on version 2021, running on Java 11. There have been many changes in both, including big changes in Java's datetime classes.
Today, there are many convenient ways to find the difference in milliseconds between any 2 dates. For example, you could proc
...Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I have spoken with HSBC and they tell me that the specific value they are looking for from the timestamp is 13 numerical characters (e.g. 1170185487375), but the value being returned from my cfset above is 1.170170719E+012, which includes non numerical characters.
By @Outside5 Ltd
An idea:
<cfoutput>#numberFormat(numberOfMilliseconds,"9999999999999")#</cfoutput>
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I've seen this a post a couple times, and I don't think it really answered the crux of the question.
Summary from above:
- dateDiff only gets you to seconds accuracy
- You can "spoof" milliseconds by multiplying seconds by 1000
- You can make sure it doesn't have power notation (E+##) using numberFormat(var,"999999999999999")
But that still doesn't tell you how to calculate the actual millisecond difference, so this is how you do that:
<!--- dtmStart and dtmEnd are your date timestamps --->
<!--- Calc Milli difference using timeFormat --->
<cfset endMilli=timeFormat(dtmEnd,"l")>
<cfset startMilli=timeFormat(dtmStart,"l")>
<cfset MillDelta=endMilli-startMilli>
<!--- Correct for negative value --->
<cfif MillDelta lt 0><cfset MillDelta+=1000></cfif>
<!--- Append milliseconds (/1000) to dateDiff Result --->
<cfset fltSecTaken = numberFormat(dateDiff("s", dtmStart, dtmEnd),"0")+MillDelta/1000 >
Copy link to clipboard
Copied
Hi all, I need to create a timestamp of difference in milliseconds between the date and time now and 00:00:00 01/01/1970.
Thanks, Paul
By @Outside5 Ltd
There is a neat answer: getTickCount().
That's it, an in-built function in ColdFusion whose return-value is an exact answer to the original question of this thread.
Test:
<cfset tickCount=gettickcount()>
<cfset secondsDiff=tickCount/1000>
As a check, the start time must be equivalent to the Unix Epoch (January 1, 1970, 00:00 GMT). <br>
<strong>
Start time (local): <cfoutput>#dateadd("s",-secondsDiff,now())#</cfoutput>
</strong>
Copy link to clipboard
Copied
Anyway, I think the most important factor to consider when you read this thread is time itself. In January 2007, when the original question was asked, ColdFusion was still on version 7, running on Java 6.
Seven releases later, ColdFusion is now on version 2021, running on Java 11. There have been many changes in both, including big changes in Java's datetime classes.
Today, there are many convenient ways to find the difference in milliseconds between any 2 dates. For example, you could proceed as follows:
<cfset N1= createobject("java","java.math.BigInteger").init("-1")>
<cfset N2 = createobject("java","java.math.BigInteger").init("1000000000000000000000000")>
<cfset result = N2.add(N1).toString()>
<cfoutput>#result#</cfoutput>
Copy link to clipboard
Copied
Based on comparing Adobe with a competing platform, I just filed the lack of support for DateDiff milliseconds as a bug. (Please upvote it so that, if updated, it will be updated on currently available versions of ColdFusion that aren't past EOL.)
https://tracker.adobe.com/#/view/CF-4217592
The following should work without throwing an error. (It works in Lucee, but wasn't documented.)
d1 = now();
sleep(15);
d2 = now();
writeoutput(datediff("l", d1, d2));
DateTimeFormat(now(), "iso") should also output millsecond accuracy or at least have an option for it... especially since platforms like SalesForce require it.
Copy link to clipboard
Copied
Voted.
Copy link to clipboard
Copied
<cfset timeStamp = dateDiff("s", createDateTime(1970,01,01,00,00,00), REQUEST.dateTimeNow)*1000>
By @Outside5 Ltd
That has a loss of accuracy, of course. You may use it if don't mind rounding off to seconds.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now