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>
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:
- Write a function getMillisFromDateTime that takes as arguments a date-time and timezone. It returns the corresponding epoch time ( in milliseconds since 01/01/1970 00:00:00 GMT). Make use of Java's new LocalDateTime and ZonedDateTime classes.
- Find the difference, deltaTMillis = getMillisFromDateTime(dateTime2, timezone2) - getMillisFromDateTime(dateTime1, timezone1);
- If ColdFusion defaults to Scientific Notation when you output the result, as was the case earlier in this thread, then use BigInteger to format the number, for example:
<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>