All right; what I would like to achieve is a function, system that counts how many (days/hours/minutes/seconds) until it reaches a specific time. I have tried to look it up but couldn’t find the words to do so. If there are any ideas, it would be very helpful!
Assuming you’re using Unix timestamp, You can try something like this:
local TargetTimeStamp = os.time() + 60*60*48; --<Two Days From Now
local s = tostring;
while wait() do
local TimeDifference = (TargetTimeStamp - os.time());
local days,hours,mins,sec = TimeDifference/86400, TimeDifference/3600, TimeDifference/60%60, TimeDifference%60;
local HumanReadable = string.format("%02dday, %02dhr, %02dmins, %02dsec,", s(days), s(hours), s(mins), s(sec));
print(HumanReadable)
end;
You don’t need to explicitally double to string conversion for string.format
for %d
pattern on Lua, all double
gets implicitally converted to string when you do anything with them. (Also use %i
as that rounds down the value instead of round up for decimal places over .5
)
Also Alvin_Blox made a live event tutorial that goes over this.
I’ll look into it, much appreciated.
You can try using os.date(), it has 2 string patterns you can put as the first parameter they are…
"*t" -- For local time (date from computer)
"!*t" -- For UTC time, recommended since it uses a global time instead of computer time.
-- This is a part that you cant really avoid, you will have to check what will be the ammount of seconds that passes from Unix EPOCH to the time you want to reach.
-- Using os.time() as a example.
local SecondsFromUNIXtoObjective = os.time()
local differenceOfTime = os.time() - SecondsFromUNIXtoObjective
--Since they are both the same ammount of time then it will have no differences
for i,v in pairs(os.date("!*t", differenceOfTime)) do
print(i,v) -- year 0, month 0, day 0, yday 0, wday 0, hour 0, min 0, sec 0, isdst false
end
In case you are confused about yday, wday, isdst then ill explain right now:
yday is the day of the year it is at (It will be 60 if 60 days has passed since the start of the year)
wday is the day of the week it is at (It will be 2 if its a monday)
isdst is a boolean that shows if daylights saving time is activated or not.
Appreciated, that what I was looking for. Although is there a possible chance to distinguish the years, months and so on? (Example: Show only hours and minutes left.) Since it’s an i,v loop, I don’t know which is which.
local differenceTimeTable = os.date("!*t", os.time() - os.time())
print(differenceTimeTable.year)
There is also another method you could try doing this, After you check the time at a epoch converter (This one works https://www.epochconverter.com/), You can just do something like this
-- DesiredTime will be the time the site gave you.
local ObjectiveDate = os.date("!*t", DesiredTime)
local CurrentDate
while wait(1) do
CurrentDate = os.date("!*t", os.time())
print("Days left: ".. (ObjectiveDate.yday - CurrentDate.yday))
end
-- Note that this will only work normally if they are in the same year.