Hey there
I made this for a script It's very simple to add more date formats, currently its only days, hours, minutes and seconds.
UPDATED 2017-02-15
Place this function in data/lib/functions.lua:
If you want to add for example weeks, you add this in table:
Also, because you added there " / 7" you need to edit the one below to " % 7":
With this you can make like quest like this:
This will tell player "Please come back in 4 days, 9 hours, 20 minutes and 51 seconds!"
Update
After the update you may now also use mtimeString(mtimeDiff) which expects time difference in milliseconds.
2 new parameters have been added to support rounding, e.g. if you do not want to display seconds, you may write timeString(diff, 60), this means 60 seconds will show as "1 minute" while 61 seconds will show as "2 minutes".
If you want a more specific rounding, you set the second third parameter as desired, e.g. timeString(diff, 60, 20), with time being 61 it will now show "1 minute" and with diff as 80 it will still show "1 minute". However, at 81 seconds it will now show "2 minutes" instead! If you want it to round up at exactly :30 seconds you write 29 as the 3rd parameter.
Using 59 as 3rd parameter would make sure it only increases the minute once it's been fully reached.
I made this for a script It's very simple to add more date formats, currently its only days, hours, minutes and seconds.
UPDATED 2017-02-15
Place this function in data/lib/functions.lua:
Lua:
function timeString(timeDiff, roundVal, roundUpVal)
if roundVal and roundVal <= timeDiff then
timeDiff = timeDiff - (roundUpVal or 0)
timeDiff = timeDiff - timeDiff % -roundVal
end
if timeDiff <= 0 then
return "none"
end
local dateValues = {
{"week", timeDiff / 60 / 60 / 7},
{"day", timeDiff / 60 / 60 / 24},
{"hour", timeDiff / 60 / 60 % 24},
{"minute", timeDiff / 60 % 60},
{"second", timeDiff % 60}
}
local result = {}
for _, dateValue in ipairs(dateValues) do
local value = math.floor(dateValue[2])
if value > 0 then
table.insert(result, ("%d %s%s"):format(value, dateValue[1], value ~= 1 and "s" or ""))
end
end
local ret = table.concat(result, ", ", 1, math.max(1, #result - 1))
if #result > 1 then
ret = ("%s and %s"):format(ret, result[#result])
end
return ret
end
function mtimeString(timeDiff, ...)
return timeString(timeDiff / 1000, ...)
end
If you want to add for example weeks, you add this in table:
Lua:
{"week", timeDiff / 60 / 60 / 24 / 7},
Also, because you added there " / 7" you need to edit the one below to " % 7":
Lua:
{"day", timeDiff / 60 / 60 / 24 % 7},
With this you can make like quest like this:
Lua:
-- Quest
player:setStorageValue(3434, os.time())
...
-- Another script
local returnTime = 5 * 24 * 60 * 60 -- 5 days
local timeLeft = player:getStorageValue(3434) + returnTime - os.time()
doPlayerSendTextMessage(cid, 22, "Please come back in " .. timeString(timeLeft) .. "!")
This will tell player "Please come back in 4 days, 9 hours, 20 minutes and 51 seconds!"
Update
After the update you may now also use mtimeString(mtimeDiff) which expects time difference in milliseconds.
2 new parameters have been added to support rounding, e.g. if you do not want to display seconds, you may write timeString(diff, 60), this means 60 seconds will show as "1 minute" while 61 seconds will show as "2 minutes".
If you want a more specific rounding, you set the second third parameter as desired, e.g. timeString(diff, 60, 20), with time being 61 it will now show "1 minute" and with diff as 80 it will still show "1 minute". However, at 81 seconds it will now show "2 minutes" instead! If you want it to round up at exactly :30 seconds you write 29 as the 3rd parameter.
Using 59 as 3rd parameter would make sure it only increases the minute once it's been fully reached.
Last edited: