I want to convert a number to be formated as days:hours:minutes
function Format(Int)
return string.format("%02i", Int)
end
function convert(Seconds)
local Minutes = (Seconds - Seconds%60)/60
Seconds = Seconds - Minutes*60
local Hours = (Minutes - Minutes%60)/60
Minutes = Minutes - Hours*60
local Days = (Hours - Hours%24)/24
Hours = Hours - Days*24
return Format(Days)..":"..Format(Hours)..":"..Format(Minutes)
end
convert(500)
However, no matter what number I enter, I always get 00:00:00 back
1 Like
function Format(Int)
return string.format("%02i", Int)
end
function convert(seconds)
local Minutes = math.floor(seconds/60)
local Hours = math.floor(Minutes/60)
Minutes = Minutes - Hours*60
local Days = math.floor(Hours/24)
Hours = Hours - Days*24
return Format(Days)..":"..Format(Hours)..":"..Format(Minutes)
end
print(convert(86400))
This seems to work for me. I only just wrote it up, but it should work fine!
6 Likes
local Minutes = math.floor(seconds/60)
seconds = seconds % 60
local Hours = math.floor(Minutes/60)
Minutes = Minutes%60
local Days = math.floor(Hours/24)
Hours = Hours%24
return (Format(Days)..":"..Format(Hours)..":"..Format(Minutes))
Blockzez
(Blockzez)
July 7, 2020, 11:50am
#5
There you go.
local function divmod(x, y) return x / y, x % y; end;
local Days, Hours, Minutes, Seconds;
Minutes, Seconds = divmod(Seconds, 60);
Hours, Minutes = divmod(Minutes, 60);
Days, Hours = divmod(Hours, 24);
return Format(Days)..":"..Format(Hours)..":"..Format(Minutes);
Still returns 00:00:00
30 characters
topcst
(topcst)
July 7, 2020, 11:56am
#8
I have a function where you simply put the number in a parameter of a function. Ill edit this post in a minute with the function
local function ToHMS(s)
return ("%02i:%02i:%02i"):format(s/60^2,s/60%60,s%60)
end
print(ToHms(500))
2 Likes
Blockzez
(Blockzez)
July 7, 2020, 11:59am
#9
Try printing out the Days
, Hours
and Minutes
to see does it print 0 just in case, because it works when I tried it.
Is there any particular reason you need your own function?
local seconds = 120320
os.date('!%H:%M:%S', seconds) --09:25:20
Blockzez
(Blockzez)
July 7, 2020, 12:28pm
#11
Isn’t the question convert number to days :hour :minutes or am I mistaken? That is hours:minutes:seconds not days:hours:minutes.
os.date('!%d:%H:%M', 1600000) --20:06:13
EDIT: I should note this only works for seconds below 2678400
. For cases above that you can use one of the solutions above or make os.date
also display the month.