WeCodeThings Library
Library of code bits
Glossary    Contact Us
Need a web designer? visit WeCodeThings.Com for more details
Search  
   
Browse by Category


WeCodeThings Library .: ColdFusion .: Helpful Functions .: ColdFusion Find Out How Long Ago a Date or String was from now



ColdFusion Find Out How Long Ago a Date or String was from now

This ColdFusion function accepts a string or a date and tell you how long ago the date is from now. It comes in handy when needing to display the a message to the user.
You can call the fucntion like this:

  1. <cfset mydate = "2009/12/03">
  2. <cfinvoke component="MyComponent" method="howLongAgo" returnvariable="returnString"
  3.          inputdate="#mydate#" />

 

Here is the function that can be called from a cfc:

  1. <cffunction name="howLongAgo" access="public" output="false" returntype="String">
  2.         <cfargument name="inputDate" required="true" type="Date" />
  3.         <cfset var datePartList = "yyyy,m,d,h,n,s" />
  4.         <cfset var datePartNameList = "year,month,day,hour,minute,second" />
  5.         <cfset var returnString = "" />
  6.         <cfset var diff = "" />
  7.  
  8.         <cfloop from="1" to="#listlen(datePartList)#" index="thisPart">
  9.                 <cfset diff = datediff(listgetat(datePartList,thisPart),arguments.inputDate,now()) />
  10.                 <cfif diff GTE 1>
  11.                         <cfset returnString = diff & " " & listgetat(datePartNameList,thisPart) />
  12.                         <cfif diff GT 1>
  13.                                 <cfset returnString = returnString & "s" />
  14.                         </cfif>
  15.                         <cfset returnString = returnString & " ago" />
  16.                         <cfreturn returnString />
  17.                 </cfif>
  18.         </cfloop>
  19.  
  20.         <cfreturn "0 seconds ago" />
  21. </cffunction>

Finally, display the output:

<cfoutput>#returnString#</cfoutput>



How helpful was this article to you?

Related Articles

article ColdFusion Figure out how long it takes to load a page using GetTickCount
Sometimes you want to see how long...

(No rating)  11-25-2009    Views: 490   
article ColdFusion Remove Quotes Single Double or Both from a String
This helpful little ColdFusion function,...

(No rating)  12-3-2009    Views: 806   
article Oracle Trunc First Day of every Month and Last Day of every Month Date
Working in Oracle:  

(No rating)  10-14-2009    Views: 813   

User Comments

Add Comment
No comments have been posted.




.: Powered by Lore .:Code by WeCodeThings Website Design and Development