Jump to content

convert string to time


#Major .

Recommended Posts

hello im working in a new script and i need help in 1 thing
im trying to convert the string to time like


 ban Player 10m

i want to convert that 10m to  time and 10h 10sec etc.
i didnt know how i can do it and split the numbers from the litters so i can know its hours or minutes etc

Edited by #Major .
Link to comment
local times = {['10m'] = 10 * 60000, ['1h'] = 60 * 60000}
-- print(times['10m'])
local timeUnits = {['m'] = 60000}
-- 10m
local number = string.match('10m', '%d+')
local unit = string.match('10m', '%a+')
-- print(tonumber(number) * timeUnits[unit])

 

Edited by nikitafloy
Link to comment
function stringToMS(str)
    local ms = 0
    str = str:lower();
    ms = ms + tonumber(str:match("(%d+)%s*s") or "0") * 1000 -- Seconds
    ms = ms + tonumber(str:match("(%d+)%s*m") or "0") * (1000 * 60) -- Minutes
    ms = ms + tonumber(str:match("(%d+)%s*h") or "0") * (1000 * 60 * 60) -- Hours
    ms = ms + tonumber(str:match("(%d+)%s*d") or "0") * (1000 * 60 * 60 * 24) -- Days
    ms = ms + tonumber(str:match("(%d+)%s*y") or "0") * (1000 * 60 * 60 * 24 * 365) -- Years
    return ms
end

 

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