Jump to content

MiliSecond to second


itHyperoX

Recommended Posts

Hello, how can i made a simple milisecToMinutes function? Something like this:

 

function secondsToMinutes(seconds)
    local totalSec = tonumber(seconds)
    if totalSec then
        local seconds = math.fmod(math.floor(totalSec), 60)
        local minutes = math.fmod(math.floor(totalSec/60), 60)
        if seconds and minutes then
            return seconds,minutes
        end
    end
end

Help me pls, i'm bad at math

Link to comment

Did you already find out?
It's easy mathematics:
1000 milliseconds are 1 second
60 seconds are 1 minute

So:
1 minute = 60 * 1000 milliseconds = 60000 milliseconds
Then you only have to round down:
 

function msToMinutes ( ms ) 
    ms = tonumber ( ms )
    if ms then
        -- 60000 ms = 1 minute
        local minutes = math.floor ( ms / 60000 )
        -- 1000 ms = 1 second 
        local restseconds = math.floor ( ms / 1000 - minutes * 60 )
        local restms = ms - restseconds * 60 - minutes * 60000 
        return minutes, restseconds, restms
    end
end

 

Edited by Bonus
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...