Skip to main content
Inspiring
January 30, 2007
Answered

Difference between 2 dates in milliseconds

  • January 30, 2007
  • 3 replies
  • 3569 views
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.

I am currently using:

<cfset timeStamp = dateDiff("s", createDateTime(1970,01,01,00,00,00), REQUEST.dateTimeNow)*1000>

but that gives me a value of 1.170170719E+012; however I require a string containing 13 numeric characters.

Any advice appreciated.

Thanks,
Paul
    This topic has been closed for replies.
    Correct answer BKBK

    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>

     

     

     

     

    3 replies

    BKBK
    Community Expert
    Community Expert
    January 29, 2022

     

     

    <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.

    Inspiring
    January 30, 2007
    Outside5.com wrote:
    > I need to create a timestamp of difference in milliseconds between the date and time now and 00:00:00 01/01/1970.

    if you're running cf7 (or maybe cf6) you can get the java epoch offset by doing
    something like:

    <cfscript>
    t=now();
    javaEpochOffset=t.getTime();
    </cfscript>

    if you really need ms accuracy be aware that dateDiff() returns at best seconds
    & you have the possibility of overflow if the dates far enough apart.
    Participant
    January 28, 2022

    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 >
    BKBK
    Community Expert
    Community Expert
    January 28, 2022

    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>
    

     

     

     

     

    Inspiring
    January 30, 2007
    > Any advice appreciated.

    All the documentation for CF is available online.

    http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000440.htm

    --
    Adam
    Inspiring
    January 30, 2007
    Thanks for the info Adam.