Convert time to seconds and seconds to time

Hello everyone! I have two very useful resource for converting time ( hour : minute : second ) into seconds ( 12345 ) and also converting seconds ( 12345 ) into time ( hour : minute : second ). So first, if you want to convert seconds into time, I will give four variables day, hour, time, seconds. If you don’t want day, you can just set it as 0.

local minutes = 0
local hours = 0
local days = 0
local seconds = 12345
repeat -- a loop
    if seconds >= 60 then -- if seconds is more than 60
	    seconds = seconds - 60 -- we take away 60 from the second
    	minutes = minutes + 1 -- and we add one to the minute
    end
	until seconds < 60 -- repeat doing this until seconds is less than 60
	repeat -- another loop
        if minutes >= 60 then -- if minutes is more than 60
    		minutes -= 60 -- we take away 60 from the minute
	    	hours += 1  -- and we add one to the hour
    	end
	until minutes < 60 -- repeat doing this until minute is less than 60
	repeat -- another loop (idk why im even explaining this)
		if hours >= 24 then  -- if hours is more than 24
	    	hours -= 24 -- we take away 24 from the hour			
            days += 1 -- and we add one to the day
	    end
	until hours < 24 -- repeat doing this until hour is less than 24

Now you have four variables : days, hours, minutes, seconds. You can customize your string anyway you like. But the most common one would be m:s. You can have hours or days but it’s uncommon so here’s how you can do it:

if days < 10 then
    days = "0"..tostring(days)
end
if days == "00" then
    days = ""
end
if hours < 10 then
    hours = ":0"..tostring(hours )
end
if hours == ":00" then
    hours = ""
end
if minutes < 10 then
    minutes = ":0"..tostring(minutes )
end
if minutes == ":00" then
    minutes = ""
end
if seconds < 10 then
    seconds = ":0"..tostring(seconds )
end
if seconds == ":00" then
    seconds = ""
end
local finalTime = days..hours..minutes..seconds

We’re finally done! Now, can make a function out of this:

    local function secondsToTime (seconds)
    local minutes = 0
    local hours = 0
    local days = 0
    local seconds = 12345
    repeat -- a loop
    if seconds >= 60 then -- if seconds is more than 60
	    seconds = seconds - 60 -- we take away 60 from the second
    	minutes = minutes + 1 -- and we add one to the minute
    end
	until seconds < 60 -- repeat doing this until seconds is less than 60
	repeat -- another loop
        if minutes >= 60 then -- if minutes is more than 60
    		minutes -= 60 -- we take away 60 from the minute
	    	hours += 1  -- and we add one to the hour
    	end
	until minutes < 60 -- repeat doing this until minute is less than 60
	repeat -- another loop (idk why im even explaining this)
		if hours >= 24 then  -- if hours is more than 24
	    	hours -= 24 -- we take away 24 from the hour			
            days += 1 -- and we add one to the day
	    end
	until hours < 24 -- repeat doing this until hour is less than 24
    if not days then
        days = 0
    end
    if not hours then
        hours = 0
    end
    if not minutes then
        minutes = 0
    end    
    if not seconds then
		seconds = 0
	end
	if days < 10 then
		days = "0"..tostring(days)
	end
	if days == "00" then
		days = ""
	end
	if hours < 10 then
		hours = ":0"..tostring(hours )
	end
	if hours == ":00" then
		hours = ""
	end
	if minutes < 10 then
		minutes = ":0"..tostring(minutes )
	end
	if minutes == ":00" then
		minutes = ""
	end
	if seconds < 10 then
		seconds = ":0"..tostring(seconds )
	end
	if seconds == ":00" then
		seconds = ""
	end
	local finalTime = days..hours..minutes..seconds
    return finalTime

There! Now we can say

print(secondsToTime(12345)) -- 3:25:45

If you want to convert time ( d : h : m : s ) into seconds, you can use the following:

function timeToSeconds (d,h,m,s)
	if d == nil then d = 0 end
	if h == nil then h = 0 end
	if m == nil then m = 0 end
	if s == nil then s = 0 end
	return ((((d * 24 + h)*60) + m)*60) + s 
end

And so it will return a number which is the seconds there are in the time. You can say:

print(timeToSeconds(0,4,39,25)) -- 16765 

There! Now you know how to convert seconds into time, time into seconds, add own customization to time and seconds and all that. For more help, join my discord channel. Gave a great day! :slight_smile:

5 Likes

This is an incredibly old way of doing it and not a good one either. When working with time you should always be working in terms of seconds. If you need larger scales of time then you should be parsing the seconds. Don’t use loops where not necessary.

All larger time scales can be derived with math. Examples are below, though I did not include rounding and all numbers work with seconds as the base value:

local seconds = 12345
local minutes = seconds/60%60
local hours = seconds/3600%24
local days = seconds/3600/24%365.25

As for the string work below, you only need string.format and zero padding on the number that you’re passing in. You don’t need so many if statements. Assuming you have integers:

local fmt = string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
6 Likes

thx it’s probably a new way of doing it

Along with what @colbert2677 said, you could also use the DateTime | Roblox Creator Documentation class. It’s a lot more versatile.

The solution he provided has been around forever. For future problems I’d suggest google so you don’t waste your time creating an inefficient way of solving something simple. Just a tip to save you some time in the future.

1 Like

Actually, no, this isn’t about working with dates. It is for many things that uses timers for rounds, unlock wait times, etc.

1 Like

Hello everyone! I have two very useful resource for converting time ( hour : minute : second ) into seconds ( 12345 ) and also converting seconds ( 12345 ) into time ( hour : minute : second ). So first, if you want to convert seconds into time, I will give four variables day, hour, time, seconds. If you don’t want day, you can just set it as 0.

local minutes = 0
local hours = 0
local days = 0
local seconds = 12345
repeat -- a loop
    if seconds >= 60 then -- if seconds is more than 60
	    seconds = seconds - 60 -- we take away 60 from the second
    	minutes = minutes + 1 -- and we add one to the minute
    end
	until seconds < 60 -- repeat doing this until seconds is less than 60
	repeat -- another loop
        if minutes >= 60 then -- if minutes is more than 60
    		minutes -= 60 -- we take away 60 from the minute
	    	hours += 1  -- and we add one to the hour
    	end
	until minutes < 60 -- repeat doing this until minute is less than 60
	repeat -- another loop (idk why im even explaining this)
		if hours >= 24 then  -- if hours is more than 24
	    	hours -= 24 -- we take away 24 from the hour			
            days += 1 -- and we add one to the day
	    end
	until hours < 24 -- repeat doing this until hour is less than 24

Now you have four variables : days, hours, minutes, seconds. You can customize your string anyway you like. But the most common one would be m:s. You can have hours or days but it’s uncommon so here’s how you can do it:

if days < 10 then
    days = "0"..tostring(days)
end
if days == "00" then
    days = ""
end
if hours < 10 then
    hours = ":0"..tostring(hours )
end
if hours == ":00" then
    hours = ""
end
if minutes < 10 then
    minutes = ":0"..tostring(minutes )
end
if minutes == ":00" then
    minutes = ""
end
if seconds < 10 then
    seconds = ":0"..tostring(seconds )
end
if seconds == ":00" then
    seconds = ""
end
local finalTime = days..hours..minutes..seconds

We’re finally done! Now, can make a function out of this:

    local function secondsToTime (seconds)
    local minutes = 0
    local hours = 0
    local days = 0
    local seconds = 12345
    repeat -- a loop
    if seconds >= 60 then -- if seconds is more than 60
	    seconds = seconds - 60 -- we take away 60 from the second
    	minutes = minutes + 1 -- and we add one to the minute
    end
	until seconds < 60 -- repeat doing this until seconds is less than 60
	repeat -- another loop
        if minutes >= 60 then -- if minutes is more than 60
    		minutes -= 60 -- we take away 60 from the minute
	    	hours += 1  -- and we add one to the hour
    	end
	until minutes < 60 -- repeat doing this until minute is less than 60
	repeat -- another loop (idk why im even explaining this)
		if hours >= 24 then  -- if hours is more than 24
	    	hours -= 24 -- we take away 24 from the hour			
            days += 1 -- and we add one to the day
	    end
	until hours < 24 -- repeat doing this until hour is less than 24
    if not days then
        days = 0
    end
    if not hours then
        hours = 0
    end
    if not minutes then
        minutes = 0
    end    
    if not seconds then
		seconds = 0
	end
	if days < 10 then
		days = "0"..tostring(days)
	end
	if days == "00" then
		days = ""
	end
	if hours < 10 then
		hours = ":0"..tostring(hours )
	end
	if hours == ":00" then
		hours = ""
	end
	if minutes < 10 then
		minutes = ":0"..tostring(minutes )
	end
	if minutes == ":00" then
		minutes = ""
	end
	if seconds < 10 then
		seconds = ":0"..tostring(seconds )
	end
	if seconds == ":00" then
		seconds = ""
	end
	local finalTime = days..hours..minutes..seconds
    return finalTime

There! Now we can say

print(secondsToTime(12345)) -- 3:25:45

If you want to convert time ( d : h : m : s ) into seconds, you can use the following:

function timeToSeconds (d,h,m,s)
	if d == nil then d = 0 end
	if h == nil then h = 0 end
	if m == nil then m = 0 end
	if s == nil then s = 0 end
	return ((((d * 24 + h)*60) + m)*60) + s 
end

And so it will return a number which is the seconds there are in the time. You can say:

print(timeToSeconds(0,4,39,25)) -- 16765 

There! Now you know how to convert seconds into time, time into seconds, add own customization to time and seconds and all that. For more help, join my discord channel. Gave a great day! :slight_smile:

3 Likes