Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua program shows current time

This is a piece of lua script that displays the time. I cannot separate the numbers ie: time.hour, ":", to basically show hh:mm:ss

time = os.date("*t")
print(time.hour .. time.min .. time.sec)
like image 923
kevintdiy Avatar asked Sep 17 '12 20:09

kevintdiy


1 Answers

There are several ways to do this:

  1. Use string concatenation: print(time.hour .. ":" .. time.min .. ":" .. time.sec)

  2. Use formatting: print(("%02d:%02d:%02d"):format(time.hour, time.min, time.sec))

  3. Use table concatenation: print(table.concat({time.hour, time.min, time.sec}, ":"))

When you really need to format your string, my preference would be for #2. For time = {hour = 1, min = 20, sec = 5} this prints:

1:20:5
01:20:05
1:20:5
like image 179
Paul Kulchenko Avatar answered Sep 18 '22 11:09

Paul Kulchenko