Matt Gifford aka coldfumonkeh | Consultant Developer
View Github Profile

Formatting Twitter Dates in ColdFusion

Dec 21, 2012

A few days ago Adam Tuttle posted a question regarding the formatting of dates returned from the Twitter API. Putting it bluntly, Twitter has screwed around with the date / time string, making it rather difficult to parse using conventional methods. During some research, other languages are also suffering from issues trying to parse the string into something useable.

Consider this example from a response:

Wed Aug 29 17:12:58 +0000 2012

Whilst ColdFusion's parseDateTime function used to work with the response strings returned from the API, it no longer does. If we pass in the string to the function, we get a date that is missing eleven years and ten hours:

{ts '2001-08-29 07:12:58'}

This prompted a few ideas bounced between Adam and I, and some rather interested method names in the process (of which I won't repeat here..)

In the end, I came up with the following function to parse and format the date into something useable:

<cffunction name="parseTwitterDateFormat" output="false" returntype="String" hint="I return the date in a useable date format.">
  <cfargument name="twitterDate" required="true" type="string" hint="The Twitter date." />
    <cfset var formatter = CreateObject("java", "java.text.SimpleDateFormat").init("EEE MMM d kk:mm:ss Z yyyy") />
      <cfset formatter.setLenient(true) />
	<cfreturn formatter.parse(arguments.twitterDate) />

This uses the SimpleDateFormat Java class, passing through the date pattern, and returning a parsed java.util.Date object.

This returns the following:

{ts '2012-08-29 18:12:58'}

The only caveat with this approach is that the SimpleDateFormat class has converted the time to my local timezone. However, it is now manageable with ColdFusion.

For any monkehTweets users out there, I have added theĀ parseTwitterDateFormat() method to the base.cfc in the latest push to the repository. If you need to use this function but dont want to download a new build, simply add the function above to your base.cfc file.

Latest Blog Posts

Jul 16, 2020
Github Actions with CommandBox and TestBox
Read More
Jul 9, 2020
Azure pipelines with CommandBox and TestBox
Read More
Dec 23, 2019
CFML content moderation detection component library
Read More