CFLib.org – Common Function Library Project

ago(dateThen)

Last updated December 7, 2009

Version: 1 | Requires: Unknown | Library: DateLib

 
Rated 2 time(s). Average Rating: 3.0

Description:
You supply a date/time, and I return a string indicating how long ago that was. e.g. "5 seconds ago", "1 year ago", etc. Useful for displaying "Last Updated" information. Does not look to the future, this is for past events.

Return Values:
Returns a string.

Example:

view plain print about
<cfset then = dateAdd('d',-1,Now())>
Then was #ago(then)#.

Parameters:

Name Description Required
dateThen Date to format. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Displays how long ago something was.
 * 
 * @param dateThen      Date to format. (Required)
 * @return Returns a string. 
 * @author Alan McCollough (amccollough@anthc.org) 
 * @version 1, December 7, 2009 
 */

function ago(dateThen){
  var result = "";
  var i = "";
  var rightNow = Now();
  Do
  {
       i = dateDiff('yyyy',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# years ago";
     break;}
  else if (i EQ 1){
     result = "#i# year ago";
     break;}

       i = dateDiff('m',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# months ago";
     break;}
  else if (i EQ 1){
     result = "#i# month ago";
     break;}

       i = dateDiff('d',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# days ago";
     break;}
  else if (i EQ 1){
     result = "#i# day ago";
     break;}

       i = dateDiff('h',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# hours ago";
     break;}
  else if (i EQ 1){
     result = "#i# hour ago";
     break;}

       i = dateDiff('n',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# minutes ago";
     break;}
  else if (i EQ 1){
     result = "#i# minute ago";
     break;}

       i = dateDiff('s',dateThen,rightNow);
       if(i GTE 2){
     result = "#i# seconds ago";
     break;}
  else if (i EQ 1){
     result = "#i# second ago";
     break;}
  else{
    result = "less than 1 second ago";
     break;
     }
  }
  While (0 eq 0);
  return result;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson