Jump to content

[HELP] Convert minutes to hours and days


Seba500PLK

Recommended Posts

Hello, my problem is that such shows. 02:60:59. Minutes and days shows correctly. Hour is displayed wrong.

  
function math.round(number, decimals, method) 
    decimals = decimals or 0 
    local factor = 10 ^ decimals 
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor 
    else return tonumber(("%."..decimals.."f"):format(number)) end 
end 
         
  
  
  
  
function formatTimeFromMinutes(value) 
  if value then 
   
    local weekday = math.floor(value / 1440) 
    local hours = math.floor(value / 60) 
    local minutes = math.round((value / 60 - math.floor(value / 60)) * 100 / 1.6666666666666667) 
     
    if weekday < 10 then 
      weekday = "0" .. weekday 
    end 
   if hours < 10 then 
      hours = "0" .. hours 
    end 
   if minutes < 10 then 
      minutes = "0" .. minutes 
    end 
    value = weekday.. ":" ..hours .. ":" .. minutes 
    return value 
  end 
  return false 
end 
  
  
  
function displaytime() 
  
  
local alivetime = getElementData(getLocalPlayer(), "alivetime") 
outputChatBox(formatTimeFromMinutes(alivetime)) 
  
end 
  

ElementData "alivetime" +1 is added every 1 minute

Link to comment

I'm not sure what you're doing, but check how i make min:seg:ms

  
                time = tostring(time) 
                ms = tostring(math.round(tonumber((time/1000 - ("%02d"):format(time/1000))*1000))) 
                min = (((time-ms)/1000)/(60)) 
                diff = (min - math.floor(min))*60 
                ms = ("%03d"):format(tostring(math.round(ms))) 
                min = ("%02d"):format(tostring(math.round(min))) 
                sec = ("%02d"):format(tostring(math.round(diff))) 

Link to comment

Have fun scripting, and read my explanation!

  
  
function formatTimeFromMinutes(value) 
  if value then 
  
    local weekday = math.floor(value / 1440) 
    local value = value - ( math.floor(weekday) * 1440 ) 
    local hours = math.floor(value/24) 
    local value = value - ( math.floor(hours) * 24 ) 
    local minutes = math.floor( value ) 
    
    weekday = ("%02"):format(tostring(math.round(weekday))) 
    hours = ("%02"):format(tostring(math.round(hours))) 
    minutes = ("%02"):format(tostring(math.round(minutes))) 
    value = weekday.. ":" ..hours .. ":" .. minutes 
    return value 
  end 
  return false 
end 
  --[[ MATH EXPLANATION 
  Eg time: 1764 
  weekday = 1764 / 1440 = 1.225 
  math.floor(weekday) = 1  
  value = value - weekday(1) * 1440 -> Remove correctly 
  value = 324 
  hours = 324 / 24 = 13.5 
  math.floor(hours) = 13 
  value = value - hours(13) * 24 -> Remove correctly 
  value = 324 - 312 = 12 
  minutes = value --> Remaining is minutes 
  Result = 1 Day : 13 Hours : 12 minutes 
  ]] 

  • Thanks 1
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...