[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2033
  • Last Modified:

Convert Seconds to hh:mm:ss

If I  have an amount of time stored as seconds in my Access dbase and I want to convert the seconds to reflect hh:mm:ss format on ColdFusion page, how is that easily done?

If I have 23400 seconds and I know that this would be 6 hours and 30 minutes, how do I get it to appear on my page as 06:30:00?
0
Lee R Liddick Jr
Asked:
Lee R Liddick Jr
  • 4
  • 2
  • 2
2 Solutions
 
mrichmonCommented:
You can do the division.

<cfset myseconds = 23400>
<cfset hrs = myseconds / 3600>
<cfset min = (myseconds mod 3600) / 60>
<cfset sec = (myseconds mod 3600) mod 60>

<cfoutput>
#hrs#:#min#:#sec#
</cfoutput>

And use NumberFormat if you want to force to be 2 digits
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
My hours are giving me 6.5...so in the above example, it's showing 6.5:30:00 on the page...
0
 
tim_csCommented:
mrichmon has the right idea but you should add SpanExcluding so things like 6.5 won't show up for hours

<cfset myseconds = 23400>
<cfset hrs =  SpanExcluding(myseconds / 3600, ".")>
<cfset min =  SpanExcluding((myseconds mod 3600) / 60, ".")>
<cfset sec =  SpanExcluding((myseconds mod 3600) mod 60, ".")>
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
mrichmonCommented:
Actually you need Fix which forces integer divisioninstead of float division :

<cfset myseconds = 23400>
<cfset hrs = Fix(myseconds / 3600)>
<cfset min = Fix((myseconds mod 3600) / 60)>
<cfset sec = (myseconds mod 3600) mod 60>

Fix takes the closest integer below the float.  SO 1.5 returns 1 and 2.4 returns 2 and 4.99999 returns 4
0
 
tim_csCommented:
You could also use the following to get the correct format. The above would give a time like 6:30:5 for 23405 but this should fix that.

<cfoutput>
#TimeFormat(createTime(hrs,min,sec), 'H:M:SS')#
</cfoutput>
0
 
mrichmonCommented:
Note you do not need to use Fix for modular division, just for the ones where you want integer division i.e. the /
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
Thanks to you both!  I had to give the majority of the points to mrichmon since he answered first and I had to give him time to respond to my second question, but your solution would have worked as well tim_cs so I increased the point total and sorted out the points to both of you!  I love this place!!!  You guys are great!  I'm having to build a web application at work with no training whatsoever in ColdFusion and everything I have learned I have learned from my SAMS book and you guys on this site...it is very much appreciated!!!
0
 
mrichmonCommented:
No problem.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now