Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lua function to convert seconds to a clock string
function SecondsToClock(seconds)
local seconds = tonumber(seconds)
if seconds <= 0 then
return "00:00:00";
else
hours = string.format("%02.f", math.floor(seconds/3600));
mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
return hours..":"..mins..":"..secs
end
end
@Hristiyanii

This comment has been minimized.

Copy link

@Hristiyanii Hristiyanii commented Oct 15, 2017

Hey, Sorry for 7 years old response, Thank you it works! :)

@diegorodriguesvieira

This comment has been minimized.

Copy link

@diegorodriguesvieira diegorodriguesvieira commented Nov 7, 2017

Hahaha, thank you man! =)

@neves768

This comment has been minimized.

Copy link

@neves768 neves768 commented Nov 26, 2017

7 fuckin' years? hahahaha lol

@RectangleEquals

This comment has been minimized.

Copy link

@RectangleEquals RectangleEquals commented Dec 5, 2017

Better late than never, I guess ¯\(ツ)

@IndianaBonesUrMom

This comment has been minimized.

Copy link

@IndianaBonesUrMom IndianaBonesUrMom commented Mar 7, 2018

Rated 5/5.

@TacticalBFG

This comment has been minimized.

Copy link

@TacticalBFG TacticalBFG commented Jun 1, 2018

8 years late but thank you for this

@ElPumpo

This comment has been minimized.

Copy link

@ElPumpo ElPumpo commented Oct 30, 2018

Hahahaha

@FrBillyD

This comment has been minimized.

Copy link

@FrBillyD FrBillyD commented Dec 17, 2018

😄 thanks !

@Majid110

This comment has been minimized.

Copy link

@Majid110 Majid110 commented Feb 7, 2019

Thank you

@SnowQT

This comment has been minimized.

Copy link

@SnowQT SnowQT commented Feb 13, 2019

never late :) 👍 thanks!

@JoBoDo

This comment has been minimized.

Copy link

@JoBoDo JoBoDo commented May 27, 2019

Hey mate 9 years later but you helped me out. Thanks!

@notela

This comment has been minimized.

Copy link

@notela notela commented Jul 8, 2019

thank you man!

@BennyFaelz

This comment has been minimized.

Copy link

@BennyFaelz BennyFaelz commented Jul 8, 2019

Nice

@chatainsim

This comment has been minimized.

Copy link

@chatainsim chatainsim commented Oct 3, 2019

Thanks!

@CarlosDSF

This comment has been minimized.

Copy link

@CarlosDSF CarlosDSF commented Oct 7, 2019

Thanks bro!

@MRePotatoes

This comment has been minimized.

Copy link

@MRePotatoes MRePotatoes commented Oct 22, 2019

now this, this is gold

@aveadrin

This comment has been minimized.

Copy link

@aveadrin aveadrin commented Nov 23, 2019

thx!!!!

@PotatoTheWorker

This comment has been minimized.

Copy link

@PotatoTheWorker PotatoTheWorker commented Mar 2, 2020

Cheers.

@ObserverOfTime

This comment has been minimized.

Copy link

@ObserverOfTime ObserverOfTime commented Apr 17, 2020

Here's a trick:

os.date('!%H:%M:%S', seconds)
-- better yet
os.date('!%T', seconds)

Explanation:

os.date ([format [, time]])

Returns a string or a table containing date and time, formatted according to the given string format.

If the time argument is present, this is the time to be formatted. [...]

If format starts with !, then the date is formatted in Coordinated Universal Time. [...]

@debagos

This comment has been minimized.

Copy link

@debagos debagos commented May 21, 2020

@ObserverOfTime
nice trick! thank you.

@string-dot-byte

This comment has been minimized.

Copy link

@string-dot-byte string-dot-byte commented Oct 5, 2020

too epick, i love and need and been searching for within centuries of my life 👌

@Yagyaveer

This comment has been minimized.

Copy link

@Yagyaveer Yagyaveer commented Oct 27, 2020

Epic! found this after 10 years lol

@Zigbigidorlu

This comment has been minimized.

Copy link

@Zigbigidorlu Zigbigidorlu commented Nov 21, 2020

10 years strong! Thanks for this! Don't have access to the "os" commands, so this worked great!

@MRePotatoes

This comment has been minimized.

Copy link

@MRePotatoes MRePotatoes commented Nov 26, 2020

holy hell you're right it's been 10 years

@nodecentral

This comment has been minimized.

Copy link

@nodecentral nodecentral commented Nov 27, 2020

Considering the time that’s passed :-)

what would the code look like for it to,also showed years, months and days too ?

@vijet1

This comment has been minimized.

Copy link

@vijet1 vijet1 commented Dec 1, 2020

Is there a way to use os.date and have custom ranges so that, in this example, hours just go on like 423:59:20?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.