• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved How to get time left to X hour.

Solution
See a bunch of useless posts in this thread, if you're not helpful just don't post lol.
Anyway, I believe there should be a better way to do it, but here's my attempt:
Code:
function timeForHour(hour)
    local time = (string.split(hour, ":")[1]*60) + string.split(hour, ":")[2]
    local current = (os.date("%H")*60) + os.date("%M")
    local diff = current < time and time - current or 1440 - (current - time)
    local hr = math.floor(diff / 60)
    local format =hr .. ":" .. math.floor(diff - (hr * 60))
    return format
end
Testing:
Code:
print("Current: " .. os.date("%H:%M"))
print(timeForHour("2:27") .. " hours left until its 2:27")
print(timeForHour("12:40") .. " hours left until its 12:40")
print(timeForHour("14:45") .. " hours left...
See a bunch of useless posts in this thread, if you're not helpful just don't post lol.
Anyway, I believe there should be a better way to do it, but here's my attempt:
Code:
function timeForHour(hour)
    local time = (string.split(hour, ":")[1]*60) + string.split(hour, ":")[2]
    local current = (os.date("%H")*60) + os.date("%M")
    local diff = current < time and time - current or 1440 - (current - time)
    local hr = math.floor(diff / 60)
    local format =hr .. ":" .. math.floor(diff - (hr * 60))
    return format
end
Testing:
Code:
print("Current: " .. os.date("%H:%M"))
print(timeForHour("2:27") .. " hours left until its 2:27")
print(timeForHour("12:40") .. " hours left until its 12:40")
print(timeForHour("14:45") .. " hours left until its 14:45")
print(timeForHour("1:31") .. " hours left until its 1:31")
Output:
Code:
Current: 14:43
11:44 hours left until its 2:27
21:57 hours left until its 12:40
0:2 hours left until its 14:45
10:48 hours left until its 1:31
 
Solution
Code:
function timeForHour(hour)
    local time = (string.split(hour, ":")[1] * 60 * 60) + (string.split(hour, ":")[2] * 60) + string.split(hour, ":")[3]
    local current = (os.date("%H") * 60 * 60) + (os.date("%M")*60) + os.date("%S")
    local diff = current < time and time - current or 86400 - (current - time)
    local hr = math.floor((diff / 60) / 60)
    local mi = math.floor(math.floor(diff - (hr * 60 * 60)) / 60)
    local se = math.floor(diff - ((mi * 60) + (hr * 60 * 60)))
    local format = hr .. ":" .. mi .. ":" .. se
    return format
end
timeForHour("HH:MM:SS") --> "HH:MM:SS"
 
Back
Top