Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Difference between 2 dates in milliseconds

Contributor ,
Jan 30, 2007 Jan 30, 2007
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
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jan 28, 2022 Jan 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> 
<
...
Translate
Community Expert , Jan 29, 2022 Jan 29, 2022

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

...
Translate
LEGEND ,
Jan 30, 2007 Jan 30, 2007
> Any advice appreciated.

All the documentation for CF is available online.

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

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 30, 2007 Jan 30, 2007
Thanks for the info Adam.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 30, 2007 Jan 30, 2007
Hi Adam,

I cannot finds anything on that page that helps. For the life of me I can't see what I am doing wrong.

Any other suggestions gang?

TIA
Paul
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 30, 2007 Jan 30, 2007
OK, one thing I just noticed about your original post:
>> I need to create a timestamp of difference in milliseconds between the date and time now and 00:00:00 01/01/1970.

The DIFFERENCE between two dates is *not* a timestamp. It is just a value.
I presume what you want is the NUMBER of milliseconds since 0:00 1/1/1970.

So by the descritpion of dateDiff() 9as per link), is this:

"Determines the integer number of units by which date1 is less than date2".

dateDiff() will not do units of milliseconds, but it will do seconds. And
there's 1000ms in a second.

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 30, 2007 Jan 30, 2007
Paul / Adam,

Thank you for your replies.

> The DIFFERENCE between two dates is *not* a timestamp. It is just a value.
> I presume what you want is the NUMBER of milliseconds since 0:00 1/1/1970.

Yes, that is true. I am integrating the HSBC card payment interface and the name of the variable they are requesting is "timeStamp". I must have just used that as a description of what I was after rather than just the name.

> "Determines the integer number of units by which date1 is less than date2".
>
> dateDiff() will not do units of milliseconds, but it will do seconds. And
> there's 1000ms in a second.

So <cfset timeStamp = dateDiff("s", createDateTime(1970,01,01,00,00,00), REQUEST.dateTimeNow)*1000> should tell me how many seconds there are between 00:00:00 on 01/01/1970 and the current date and time, then the *1000 should tell me the value in milliseconds?

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.

The HSBC payment server is on a Linux server and my host is Windows. Are there any differences in the formatting of such a string?

I cannot see anything I have missed on this.

Any other suggestions?

Thanks,
Paul
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2007 Jan 30, 2007
Java's currentTimeMillis()

milliseconds between the current time and midnight, January 1, 1970 UTC = <cfoutput>#createobject("java","java.lang.System").currentTimeMillis()#</cfoutput>


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2007 Jan 30, 2007

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>



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 30, 2007 Jan 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 28, 2022 Jan 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 >
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2022 Jan 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>

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2022 Jan 29, 2022

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>

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 06, 2023 Apr 06, 2023

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. 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2023 Apr 06, 2023
LATEST

Voted.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2022 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources