CFLib.org – Common Function Library Project

ago(dateThen)

Last updated December 07, 2009

author

Alan McCollough

Version: 1 | Requires: | Library: DateLib

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:

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

Parameters:

Name Description Required
dateThen Date to format. Yes

Full UDF Source:

/**
 * 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson