PDA

View Full Version : Function: Convert seconds into minutes/seconds formatted for display, i.e. "2:30"


Corey
01-18-2006, 02:15 AM
Simply insert this function into your project then call it as you would any normal function, example below:

function sec2Min(secs)

if secs > 59 then

myMinutes = Math.Floor(secs/60);
mySeconds = secs-(Math.Floor(secs/60)*60);

if mySeconds < 10 then
mySeconds = "0"..mySeconds;
end

myTime = myMinutes..":"..mySeconds;

else

if secs < 10 then
mySeconds = "0"..secs;
end

myTime = "0:"..mySeconds;

end

return myTime;

end

Example call to convert 75 seconds into formatted "m:s" time:

result = sec2Min(75);
Dialog.Message("Time:", result);

TJ_Tigger
01-20-2006, 11:06 AM
You can also use the string.format function which is part of lua to zero fill a variable.

mySeconds = string.format("%2f", secs-(Math.Floor(secs/60)*60);t);

tigg

Intrigued
01-20-2006, 05:25 PM
You can also use the string.format function which is part of lua to zero fill a variable.

mySeconds = string.format("%2f", secs-(Math.Floor(secs/60)*60);t);

tigg

I'm going to use this instead of my code I have been using.

Right on 'Tigg!

Corey
01-20-2006, 09:39 PM
Yep. To quote the late great Ed Whalen from Stampede Wrestling, "That's a ring-a-ding-dong dandy". :yes

Eagle
01-21-2006, 12:19 PM
ditto , tks Tigg

(and for yours Mr Corey)

TJ_Tigger
01-26-2006, 08:33 AM
I didn't test the code above but here is a funciton that will convert seconds to HH:MM:SS.


function SecondsToClock(sSeconds)
local nSeconds = String.ToNumber(sSeconds)
if nSeconds == 0 then
--return nil;
return "00:00:00";
else
nHours = string.format("%02.f", Math.Floor(nSeconds/3600));
nMins = string.format("%02.f", Math.Floor(nSeconds/60 - (nHours*60)));
nSecs = string.format("%02.f", Math.Floor(nSeconds - nHours*3600 - nMins *60));
return nHours..":"..nMins..":"..nSecs
end
end


Tigg

Intrigued
08-22-2006, 08:31 PM
I didn't test the code above but here is a funciton that will convert seconds to HH:MM:SS.


function SecondsToClock(sSeconds)
local nSeconds = String.ToNumber(sSeconds)
if nSeconds == 0 then
--return nil;
return "00:00:00";
else
nHours = string.format("%02.f", Math.Floor(nSeconds/3600));
nMins = string.format("%02.f", Math.Floor(nSeconds/60 - (nHours*60)));
nSecs = string.format("%02.f", Math.Floor(nSeconds - nHours*3600 - nMins *60));
return nHours..":"..nMins..":"..nSecs
end
end


Tigg

Thanks 'Tig, little late maybe, but it saves me from coding such. :yes

Worm
08-22-2006, 08:58 PM
don't forget DateDiff does this too :)

http://www.indigorose.com/forums/showpost.php?p=62913&postcount=7

Intrigued
08-22-2006, 09:06 PM
Ah, ha Ah, ha ha.

Thanks Worminator, "not just a Terminator, but a Worminator sort of Terminator!" "Stamping out problematic code chunks in a single leap!"

:D

Tek
11-24-2006, 07:02 PM
I have a need to get a number in milliseconds and convert it to 'HH:MM:SS.t' where 't' is rounded to tenths of a second.

Can this function be used to do that?

TJ_Tigger
11-24-2006, 11:56 PM
should be able to. Just need to adjust the math so it calculates from miliseconds rather than seconds

function MilliSecondsToClock(sMilliSeconds)
local nMilliSeconds = String.ToNumber(sMilliSeconds)
if nMilliSeconds == 0 then
--return nil;
return "00:00:00";
else
nHours = string.format("%02.f", Math.Floor(nMilliSeconds/3600000));
nMins = string.format("%02.f", Math.Floor(nMilliSeconds/60000 - (nHours*60000)));
nSecs = string.format("%02.f", Math.Floor(nMilliSeconds/1000 - nHours*3600000 - nMins *60000));
nMilliSecs = Math.Round(nMilliseconds - (nHours*3600000) - (nMins *60000) - (nSecs *1000))
return nHours..":"..nMins..":"..nSecs.."."..nMilliSecs
end
end

Or something close to that anyway.

HTH
Tigg