Working with Unix Time Stamps in ColdFusion

Recently I wrote a little utility to auto import flickr photosets into the content management system we user over at CreativEngine.

Turns out the flickr api returns Unix time stamps for its datetime data, e.g.

{ "date_create": "1384369643", "date_update": "1384369647" }

Since Unix time stamps are the number of seconds that have elapsed since January 1st, 1970 00:00:00 UTC, the solution consisted of:

  1. creating a datetime object for 01-01-70 00:00:00

  2. Using the Coldfusion DateAdd function to add the date coming in from flickr to the base date, and

  3. using the DateFormat and TimeFormat functions to format the resulting datetime


<cfset baseDate = createdatetime(1970, 1, 1, 0, 0, 0) />
<cfset createdRaw = dateadd("s", photoset.date_create, baseDate) />
<cfset created = dateformat(createdRaw, "yyyy-mm-dd") & " " & timeformat(createdRaw, "hh:nn:ss") />