Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a function to convert an Integer in hour:minute:second format

Status
Not open for further replies.

bricenoej

Programmer
Mar 19, 2002
50
VE
Hi everybody...

I would like to convert an Integer in hour:minute:second format... Is there a way for do that???

Thanks everybody...
bricenoej
 
Hi tleish... I am sorry... I didn't get back to you soon.
I have an integer number that contains a value in seconds, for example: if the value is 72, its means 72 seconds, and I would like to convert this 72 seconds in 00:01:12

Greetings and Thank you... bricenoej
 

Regular Version:
=== START CODE EXAMPLE ===
<cfscript>
seconds = 72;
midnight = CreateTime(0,0,0);
time = DateAdd(&quot;s&quot;, seconds, variables.midnight);
</cfscript>

<cfoutput>
#TimeFormat(variables.time, 'HH:mm:ss')#
</cfoutput>
=== END CODE EXAMPLE ===

Compact Version:
=== START CODE EXAMPLE ===
<cfset seconds = 72>
<cfoutput>
#TimeFormat(DateAdd(&quot;s&quot;, seconds, CreateTime(0,0,0)), 'HH:mm:ss')#
</cfoutput>
=== END CODE EXAMPLE === - tleish
 
Thank you very much tleish...
Perfect.. I used the second one.
Greetings....Eduardo
 
Hi tleish!

I have a little trouble with the formulas. For example, when the seconds are greater than 86400 (A day 24hours) the result is 00:00:00. If we have 86460 (seconds) the result is 00:00:60 and should be 24:00:60... Is there a way for do that?

I really apreciate your help!

Greetings.. bricenoej
 
Here's an idea, although I can't test it because we're currently still using ColdFusion 4.5. What I've done is write a recursive function called hmsFormat() that takes care of this formatting.
Code:
<cfscript>
  function hmsFormat(inp)
  {
    if(ArrayLen(Arguments) GT 1)
    {
      var str = &quot;:&quot; & Arguments[2];
    }
    else
    {
      var str = &quot;&quot;;
    }
    if((inp LT 60) OR (ListLen(inp,&quot;:&quot;) EQ 2))
    {
      return inp & str;
    }
    var a = inp mod 60;
    var b = (inp - a)/60;
    if(a LT 10)
    {
      a = &quot;0&quot; & a;
    }
    return hmsFormat(b,a & str);
  }
</cfscript>
If you had an integer variable int (for example, 92768),
Code:
#hmsFormat(int)#
would return the formatted hh:mm:ss string (in this example, &quot;25:46:08&quot;).

Somebody please let me know if this works -- as I said, I can't test it because I'm still using ColdFusion 4.5.
 
Try this:

=== START CODE EXAMPLE ===a
<cfscript>
seconds = 86460 ;
midnight = CreateTime(0,0,0);
time = DateAdd(&quot;s&quot;, seconds, variables.midnight);
hours = Int(seconds / 3600);
[COLOR=666666]// Add 0 Left padding for hours under 10[/color]
if( variables.hours lt 10 ) hours = 0 & variables.hours;
</cfscript>

<cfoutput>
#variables.hours#:#TimeFormat(variables.time, &quot;mm:ss&quot;)#
</cfoutput>

=== END CODE EXAMPLE === - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top