Convert ColdFusion Date to Unix timestamp
Asked Answered
B

3

7

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

Bonkers answered 19/8, 2019 at 17:42 Comment(4)
There's a UDF for that at CFLib... getEpochTime(). cflib.org/udf/GetEpochTimeSchulein
Do you want this to be the timestamp from your application server or from your database server? What version of CF? And how is this being used? If you're going to do date math, you want to make sure all of your dates are starting from the same source and same point. Or at least you know how to convert them to a useful point of reference.Birthplace
Edit: On a geeky note, this also means that in a little over 18 years, a lot of computers are going to forget how to accurately track time. Y2K again in Y2K38. :-)Birthplace
Also, to add to the fun, ColdFusion doesn't use 1970-01-01 as Epoch. It uses 1899-12-30. trycf.com/gist/a9852ba8f37c68b11898482cda0cc2a8/…Birthplace
A
11

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 )>
Atthia answered 19/8, 2019 at 18:11 Comment(6)
What does it need to be 1970? I want to get the last 30 days. I mean, Now() - 30 daysBonkers
The start date is always 1970 because that's the Unix Epoch - what a Unix timestamp is based on. If you want to just get the # of seconds in the last 30 days, or the Unix time of a date 30 days in the past, you can alter the variables above to do that.Atthia
Why only one dateConvert?Futile
@Ageax Good question. Running the start date through that function does not change the final result. It is absolute.Atthia
@Ageax Also note that Unix Epoch is based on UTC, so don't need to convert it there. Note, too, that using 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
@Birthplace - True. I know it's supposed to be be based on utc, but ... I recall CF getting creative with some of the utc stuff. Thought it might be a fix for something :-) "Never mind. Carry on."Futile
R
4

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 );
Rump answered 13/9, 2020 at 1:4 Comment(0)
F
1
<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.

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-e-g/gettickcount.html

Futurism answered 6/10, 2023 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.