The Official Salesforce Blog

The Official Salesforce Blog

Converting Date/Time function output into HH:MM:SS

Kingsley Joseph Apr 25, 2006

Spazure Silicon blogged this tip on his/her blog with the caveat that "So, even though this formula won't ever be on the CRM Best Practices Custom Formula blog, I hope somebody finds it, and finds it useful." So, mainly to prove him/her wrong, but also because it's very useful, here it is - a function to convert Date/Time into HH:MM:SS.

This particular formula was designed for cases, calculating the elapsed time in HH:MM:SS between right now and the time the case was created. As you see, I had to use the TEXT() function to convert the numerically output Date/Time function results into text, which were then fed into the Concatenate function to make everything display nicely on one field. My original version of this formula actually used multiple fields, {!Hours} {!Seconds} {!Minutes}. This made the final formula look prettier, and helped with debugging, but ultimately was impractical for distribution.

So, even though this formula won't ever be on the CRM Best Practices Custom Formula blog, I hope somebody finds it, and finds it useful. If you use my formula, please comment -- not because I want credit, but because I really do like to know where my code ends up. :-)

Text(FLOOR((NOW() - {!CreatedDate}) * 24))

&":"&Text(FLOOR(((NOW() - {!CreatedDate}) * 24 - FLOOR((NOW() - {!CreatedDate}) * 24))* 60))

&":"&Text(FLOOR((( (NOW() - {!CreatedDate}) * 24 - FLOOR((NOW() - {!CreatedDate}) * 24 )) * 60 - FLOOR(((NOW() - {!CreatedDate}) * 24 - FLOOR((NOW() - {!CreatedDate}) * 24 )) * 60 )) * 60 ))

 

6 Comments

Rachelle

This is a good formula, but most people usually want to track the elapsed time from when the case was created to when it was set to In Progress or Closed. This formula wont work there because it uses the NOW() which will increment the time output each time the record is accessed.

Spazure

You could always work with closed date and created date, if you'd prefer. That's a bit more complicated, but do-able. My blog has several variations of date and date/time formulas at http://www.ihatethebus.com.

Jon

This seems to work great except under certain circumstances. For example, when there is exactly 1 hour difference it actually calculates at 59 minutes. For example a difference between 11:00am and 12:00pm is 0.59, and between 11:00am and 12:01pm is 1.0, between 11:00am and 12:02pm is 1.1, but then between 11:00am and 12:03pm is 1.3
Weird...I think it may have something to do with when it's dividing by zero it doesn't calculate properly.

Kavita

This works fabulously for me. I needed to track exactly how much time has lapsed between now and created date. I do need other time calculations but I will try to work off this.

Brettnnyc

If you want to use a calculation to convery minutes correctly take each minute and divide it by 60 and you'll get the proper decimal increment that the minutes represent. Not sure if that little bit helps or not.

John

Note that the formula above does not have a "IF (IsClosed , ..." function - so this Case "shotclock" will keep running after a Case is closed (not very ideal). Most folks will want some type of syntax indicating to calculate Now() - CreateDate only if the Case is open, but if Case is closed ClosedDate - CreatedDate.

Post a comment