You have a date and time in Epoch seconds that you would like to convert to a date/time object.
Use DateAdd() to add the Epoch seconds to the Epoch date.
ColdFusion does not natively deal with dates based on the Epoch. However, as a developer, you may be faced with situations where you are provided with a date/time value stored in Epoch seconds. If this is the case, you can easily convert the value to a ColdFusion date/time object using the DateAdd() function.
<cfoutput>
#DateAdd("s",e,DateConvert("utc2Local", "January 1 1970 00:00"))#
</cfoutput>
In this example, the Epoch is first converted to local time using the DateConvert() function. Next, the number of Epoch seconds we have are added to the converted Epoch time using DateAdd(). The output looks like this:
Using the DateConvert() function is only necessary if you want to convert Epoch seconds to local time. You can leave it out if you simply want Epoch seconds converted to a date/time object in UTC.
This question was written by Rob Brooks-Bilson.
It was last updated on January 6, 2006 at 1:10:39 PM EST.
CFML Referenced
<cfoutput>
DateConvert()
DateAdd()
Categories
Comments
Comment made by Matthew Reinbold on February 22, 2006 at 6:40 PM
Just a quick 'gotcha' about this. The example value for 'e' is 10 numbers long. This converts just fine. However, if you happen to be fetching a epoch from java, say, for the lastModified() date like the following:
<cfset objJava = createObject("java","java.io.File") /> <cfset fileJava = objJava.init(cgi.PATH_TRANSLATED) />
then the epoch value that comes back is 13 numbers long and throws the following when trying the DateConvert:
"Could not convert the value 1.140651238729E12 to an integer because it cannot fit inside an integer."
I assume its because Java is attempting to also return milleseconds(?) which isn't necessary in most cases. I added the following line:
<cfset javaDate = Left(fileJava.lastModified(),10) />
and things worked fine.
Add a Comment