How can I convert a ColdFusion date to Unix Timestamp?
myDate = DateAdd("m", -1, Now());
I would like to convert myDate to unix timestamp using ColdFusion
Thanks
How can I convert a ColdFusion date to Unix Timestamp?
myDate = DateAdd("m", -1, Now());
I would like to convert myDate to unix timestamp using ColdFusion
Thanks
Just create the UNIX origin date, and do a DateDiff from then to now (or whatever your date variable is) in seconds.
<cfset startDate = createdatetime( '1970','01','01','00','00','00' )>
<cfset datetimeNow = dateConvert( "local2Utc", now() )>
<cfset UnixStamp = datediff( 's', startdate, datetimeNow )>
dateConvert("local2UTC",mydate)
will base the calculations on the time of the computer the application is running on (for instance Daylight Savings Time). Dates in computers get really "interesting". There's not really such a thing as 2019-08-19 12:42:33 PM
; the computer sees it as 1566218553
. Which makes date math easy, but "fun" sometimes. –
Birthplace Since CF stores DateTime objects as Java Date objects, this also works:
var unixStamp = int( now().getTime() / 1000 );
getTime() returns milliseconds, which is why I divided by 1000 and rounded using int().
So, specifically for the example in the OP:
var unixStamp = int( myDate.getTime() / 1000 );
<cfset unixStamp = int(getTickcount() / 1000) />
You can get the current value of the internal milliseconds timer, and divide that by 1000
to get the time in seconds. Optionally you can cast to an integer if you don't want a float.
© 2022 - 2024 — McMap. All rights reserved.
1970-01-01
as Epoch. It uses1899-12-30
. trycf.com/gist/a9852ba8f37c68b11898482cda0cc2a8/… – Birthplace