ColdFusion Relative Time Script UDF

This post is more than 2 years old.

A few minutes ago a user in the ColdFusion IRC channel asked about an ActionScript relative time script. What is a relative time script? It translates something like "December 10" to "8 days ago". Turns out Google found a bunch of examples here. I took the first example, a JavaScript one, and rewrote it. This version is ColdFusion 8 only since it uses <, but you could change that in about 2 nanoseconds for a cf7 and earlier version. I'll add this up to CFLib a bit later probably.

<cfscript> function relativeTime(pastdate) { var delta = dateDiff("s", pastDate, now());
if(delta &lt; 60) {
    return "less than a minute ago";
} else if(delta &lt; 120) {
    return "about a minute ago";
} else if(delta &lt; (45*60)) {
    return round(delta/60) & " minutes ago";
} else if(delta &lt; (90*60)) {
    return "about an hour ago";
} else if(delta &lt; (24*60*60)) {
	return round(delta/3600) & " hours ago";
} else if(delta &lt; (48*60*60)) {
    return "1 day ago";
} else {
	return round(delta/86400) & " days ago";

} </cfscript>

<cfset pastdate = dateAdd("n", -9400, now())> <cfoutput>Paris had dignity #relativeTime(pastdate)#</cfoutput>

I'll update the wiki too (got to represent the ColdFusion!)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Will Swain posted on 12/19/2007 at 10:13 PM

Thanks Ray,

However, I'm concerned that your example is redundant. Not sure Ms Hilton ever had dignity! :)

Comment 2 by ziggy posted on 2/27/2008 at 7:48 AM

This syntax errors in BD7 too. Found out using Project Tracker which incorporated it and couldn't be started.

For the sake of one character, best to change all "<" to "LT" so it works in everything.

Comment 3 by Raymond Camden posted on 2/27/2008 at 7:54 AM

Well, I did clearly point out it was cf8 only because of the <. Folks are welcome to mod it.

Comment 4 by Steve Glachan posted on 6/25/2009 at 5:11 AM

Thanks Ray, I'm now using this in a Twitter RSS feed reader. Mixed in GetTimeZoneInfo() to get our Australian offset hours and it's working great!

Comment 5 by Misty posted on 7/19/2014 at 11:04 PM

Hi ray, Coming back to this post very late,

I am using the UDF from cflib for the timezone:

I have included the weeks also in this:

else if (dateDiff("w", now(),{
offset = dateDiff("w",now(),;
interval = offset == 1 ? "week":"weeks";
result = "#offset# #interval# left";

working okay, but week is off 7 days and if the date with which i am comparing is say 9 days, i want this to be appearing as 1 week 2 days, i am missing something, not sure what, can you guide

Comment 6 by Raymond Camden posted on 7/20/2014 at 1:48 AM

I'm not quite sure I get your question here. It sounds like you are saying the week difference is off by 7 days - or by one?

Comment 7 by Misty posted on 7/20/2014 at 11:28 AM

Hi Ray, Here is the Pastebin, It is working as of now, but i have removed the weeks as they were not giving me correct results, so what i am actually trying is:

5 days left or 5 days ago

1 week 3 days left

2 months 10 days left

if for specific month, days are 30 or 31, it should say 1 month.

I hope you can guide me what changes can be made to fix these above things:

Comment 8 by Raymond Camden posted on 7/21/2014 at 4:35 AM

I honestly don't know what your doing in that code, but here is a modified version of my code that supports weeks. It is not heavily tested.