Title says it all. Anyone know how I can make this?
Title says it all. Anyone know how I can make this?
Maybe like this:
local p = {}
local function toDHMS(millis)
local seconds = millis % 60000 / 1000
local minutes = millis % 3600000 / 60000
local hours = millis % 86400000 / 3600000
local days = millis / 86400000
return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end
function p.toDHMS(frame)
local args = require("Module:Arguments").getArgs(frame)
local millis = args[1] or error("missing positional argument #1: milliseconds")
return toDHMS(millis)
end
return p
Put the code for example in Module:ConvertTime and then you can invoke it:
{{#invoke:ConvertTime|toDHMS|23456576210}}
This should give you the following output:
271:11:42:56
Oh, wow. Didn't think someone would actually respond. Thank you!
It actually "works", but it seems to display an error before the converted time. I edited the output slightly to fit my wiki, but that could be why the error displays in the first place.
Yes, sorry, my bad. I just tested this module in the debug console and didn't invoke it from a regular page…
Try modifying the function p.toDHMS like so:
function p.toDHMS(frame)
local args = require("Module:Arguments").getArgs(frame)
if args[1] == nil then
error("missing positional argument #1: milliseconds")
end
local millis = tonumber(args[1])
if millis == nil or millis < 0 then
error("invalid argument: " .. args[1])
end
return toDHMS(millis)
end
Cool, it works! Thanks again.
Another question, any way to hide the numbers if it returns 0?
You just glue the parts together that are not zero:
local function toDHMS(millis)
local result = ""
local days = millis / 86400000
if days >= 1 then
result = result .. string.format("%dD", days)
end
local hours = millis % 86400000 / 3600000
if hours >= 1 then
result = result .. string.format(" %dhr", hours)
end
local minutes = millis % 3600000 / 60000
if minutes >= 1 then
result = result .. string.format(" %dmin", minutes)
end
local seconds = millis % 60000 / 1000
if seconds >= 1 then
result = result .. string.format(" %dsec", seconds)
end
return result
end
Got it. Thank you once again
What do you think?