Please add a datetimeformat mask that returns epoch time

Mask = secFromEpoch or msFromEpoch maybe?

I’m sure there is an arcane way to do this, but if I want a date/time formatted a specific way I gravitate towards the obvious…

The getTickCount() function returns the system time in milliseconds. In ACF I remember dividing that by 1000 and converting to an integer, but that’s not necessary in Lucee because you can also pass in an argument to get that number in nanoseconds or seconds;

For your use case you can probably use getTickCount("second"). If you compare the result to what you see on https://www.epochconverter.com/ they should match :slight_smile:

1 Like

I want to format a date coming from a query.

Can’t you just use.getTime()?

1 Like

Your link didn’t work. Does that function even exist? I don’t see it here: https://docs.lucee.org/reference/functions.html

If so, maybe it will return epoch time from a datetime object, but I still feel that since I’m just looking for the datetime formatted differently, adding the functionality to datetimeformat() would be most useful and intuitive rather than having to keep track of yet another single-use time formatting function.

6 months from now when I need an epoch time string again, for highcharts, for example (they need milliseconds btw), the first thing I’m going to think of is datetimeformat which is designed to convert a datetime object into a different format.

It should be an easy addition.

There seems to be a problem with Lucee 5 on trycf.com, changing the above link to Lucee 4.5 fixes it;

getTime() is a method on Java date objects so it should be ok to use. There’s no Lucee built-in function equivalent at the moment that I know of, I have always resorted to using DateDiff() with seconds from the epoch time for converting existing dates.

I appreciate the pointer and that will get me past the problem today.

I still believe that adding this as a separate “mask” in datetimeformat() would be the most useful option as that is the first place that most people will look when trying to figure out how to format a datetime in seconds/milliseconds from 1970. One of the primary strengths of CF is its intuitive nature. That this function exists in Java, and that it can be used directly in CF, is arcane knowledge and most people aren’t just going to stumble onto it easily.

And since adding it should be as simple as the following, why wouldn’t you?

if (mask == 'epoch'){
  return inputDate.getTime()/1000;
} else if (mask == 'epochMS'){
  return inputDate.getTime();
}
1 Like

Sure, they could add the mask to datetimeformat() but it takes a Date Object, and you can easily get the time from it. Personally, I don’t think the mask is necessary.

1 Like

Feel free to ignore it if they add it, then :wink:

I’m glad you can keep track of all of these workarounds. It’s not as easy for some of us, which is why I use CF instead of .net, java, c++ etc.

I’m in favor of adding a mask like “epoch”. I think that it can be useful.

4 Likes

+1

Support for the JavaScript style iso format would also be super useful. https://luceeserver.atlassian.net/browse/LDEV-2131

2 Likes
 <cffunction name="epochTime" output="false" access="public" returnType="string">
        <cfargument name="mydatetime"  type="date"  required="false" default="" />
            <cfscript>
                // set the base time from when epoch time starts
                startDate = createdatetime( '1970','01','01','00','00','00' );	
                // time zone check if its dyalight savings forward add 1 hr. 
                cfTZ = getTimeZoneInfo(); 
                dateObj = mydatetime; 
                if (cfTZ.isDSTOn) { 
                    mydatetime = dateAdd('h', 1, mydatetime); 
                } 
        
                datetimeNow = dateConvert( "local2Utc", mydatetime );
                return datediff( 's', startdate, datetimeNow );
            </cfscript>
    </cffunction>

<cfoutput>#epochTime(mydatetime="#now()#")#</cfoutput>

File a bug :slight_smile:

https://luceeserver.atlassian.net/browse/LDEV-2439

Thanks @nalbee - that is the perfect epoch time udf :wink: If the epoch mask existed you wouldn’t have had to write that!

1 Like

I wish I could upvote the javascript iso issue in Atlassian - I got bit by this too.

try the old view
https://luceeserver.atlassian.net/browse/LDEV-2131?oldIssueView=true