generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Creating hh:mm:ss formatted time

Previous Thread :: Next Thread 
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
04 Dec 2012 01:48 PM
I remember that there is a fantastic way to make sure each component has exactly two characters, using something like string.format.
I could easily do this without the method, but I MUST BE EFFICIENT O_e

Example:

local function hhmmss(seconds)
local s=seconds%60
local m=(seconds-s)/60%60
local h=(seconds-m*60-s)/3600%24
return (h>0 and h..":" or "")..(h>0 and (m)..":" or m>0 and m..":" or "")..((h>0 or m>0) and (s) or s)
end

Yes, that's fine and dandy, but at 1 hour, 1 minute, and 1 second (3661 seconds):

print(hhmmss(3661))

> 1:1:1

the time should be formatted 1:01:01, not 1:1:1.
This is easily fixed with if m<10 then "0"..m, but that's why I am asking, because I know I have seen a better way.
Basically, the terms '(m)' and '(s)' need to be formatted.
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
04 Dec 2012 02:11 PM
You could do

string.rep("0",2-#m) .. m

Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
04 Dec 2012 02:14 PM
That's what I usually do, there's something better. It's probably on NXTboy's wikipage... Going there.
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
04 Dec 2012 02:22 PM
Could it be string.format("%2.f",h)?
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
04 Dec 2012 02:29 PM
oh well :(
I'll just use

    local zero="0"
    zero:rep(1-(m>0 and math.floor(math.log10(m)) or 0))..m

Lol jk
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
04 Dec 2012 02:36 PM
m = string.rep("0",#(m..""))..m

shortens it up a lot, and makes it easier to understand
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
04 Dec 2012 02:44 PM
I did say loljk :P
I've put in

    zero:rep(2-#tostring(m))..m
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jan 2013 04:55 PM
Necrobump, kinda, but since you never got your answer:

    ("%02d"):format(m)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image