Hey! I’m looking for a simple way to convert a number into minutes:seconds:milliseconds
My numbers look like this:
Thanks!
Hey! I’m looking for a simple way to convert a number into minutes:seconds:milliseconds
My numbers look like this:
Thanks!
I made something for this in the past, but it doesn’t support ms.
What’s your use case for this if you mind me asking?
Simple script, returns a tuple of the minutes, seconds, and milliseconds.
function secsToTime(timeInput)
local minutes = math.floor(timeInput / 60)
local secs = math.floor(timeInput%60)
local miliseconds = timeInput-math.floor(timeInput)
miliseconds = math.round(miliseconds*1000)
return minutes, secs, miliseconds
end
-- example
local mins, secs, ms = secsToTime(60.120)
Or here is another version, formated exactly like how you wanted, example 5:21:931
function secsToTime(timeInput)
local minutes = math.floor(timeInput / 60)
local secs = math.floor(timeInput%60)
local miliseconds = timeInput-math.floor(timeInput)
miliseconds = math.round(miliseconds*1000)
return minutes..":"..secs..":"..miliseconds
end
This works perfectly, thank you!
I formated it like this
string.format("%02i:%02i:%02i", minutes, secs, miliseconds)
k nice, i just don’t really understand the format string block, only code i don’t get. it could be i spent like 5 mins studying it but what ever
apologies for necroposting, but is there a way to put a 0 within the milliseconds if it hasn’t reached 100 yet? I want to try making a speedrun timer but that little detail kind of bumps the text every second.
i think you can just change the milisecond format to %03i
Wow, that was quick. That actually worked pretty well, thanks!
yeah, i just thought that since the original format shows 2 zeros which I assume is represented by the 2 in %02i (the i
means integer)
Use this script function ConvertToTime(Number : number)
if tonumber(Number) then
local Time = {
['Second'] = 0,
['Minutes'] = 0,
['Hours'] = 0,
['Days'] = 0,
['Week'] = 0,
['Month'] = 0,
['Years'] = 0,
}
if Number >= 60 then
repeat
Time.Minutes += 1
Number -= 60
wait()
until Number < 60
Time.Second = Number
if Time.Minutes >= 60 then
Number = Time.Minutes
repeat
Time.Hours += 1
Number -= 60
wait()
until Number < 60
Time.Minutes = Number
if Time.Hours >= 24 then
Number = Time.Hours
repeat
Time.Days += 1
Number -= 24
wait()
until Number < 24
Time.Hours = Number
if Time.Days >= 7 then
Number = Time.Days
repeat
Time.Week += 1
Number -= 7
wait()
until Number < 7
Time.Days = Number
if Time.Week >= 4 then
Number = Time.Week
repeat
Time.Month += 1
Number -= 4
wait()
until Number < 4
Time.Week = Number
if Time.Month >= 12 then
Number = Time.Month
repeat
Time.Years += 1
Number -= 12
wait()
until Number < 12
Time.Month = Number
end
end
end
end
end
end
return {
['Time'] = Time,
['Convert'] = function(Time)
local Clock = tostring(Time.Years.." Years : "..Time.Month.." Month : "..Time.Week.." Week : "..Time.Days.." Days : "..Time.Hours.." Hours : "..Time.Minutes.." Minutes : "..Time.Second.." Second")
return Clock
end,
}
end
end
local Time = ConvertToTime(500)
print(Time.Convert(Time.Time))