String format H:M:S (hour:minute:second)

How to convert a time in milliseconds in a correct string format? (cf. I searched a lot in lua-users.org/wiki/DateAndTime, but certainly I missed something:

[lua]local t = system.getTimer()
print(string.format ("%02H:%02M:%02S", t)) --??doen’t work[/lua]

The goal is to get: 00:00:00 running. Thanks for help.

[import]uid: 8970 topic_id: 5924 reply_id: 305924[/import]

I assume you’re talking about an elapsed time in milliseconds?

If you want the current time, you can do this:

local date = os.date("\*t")  
for i,v in pairs(date) do  
 print(i.."="..v)  
end  

For elapsed time it’s more complicated. So let’s say you’ve done something like this:

local t1 = system.getTimer()  
-- ...  
-- some stuff happens here  
-- ...  
local t2 = system.getTimer()  
local ms = t2 - t1  

In order to display this in HH:MM:SS format, you need to break it down into its components. Here’s some code you could use:

local t1 = system.getTimer()  
  
--for i = 1, 1000000000 do  
-- local x = i \* i  
--end  
  
local t2 = system.getTimer()  
  
-- For this example, I'm going to pretend we got t1 a while ago.  
-- Let's say it was 10 million milliseconds ago.  
t1 = t2 - 10000000  
  
local ms = t2 - t1  
print("elapsed milliseconds = " .. ms)  
  
local floor = math.floor  
local seconds = floor(ms / 1000)  
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)  
local hours = floor(minutes / 60); minutes = floor(minutes % 60)  
  
formattedTime = string.format("%02d:%02d:%02d", hours, minutes, seconds)  
print("elapsed time = " .. formattedTime)  

[import]uid: 9659 topic_id: 5924 reply_id: 20802[/import]

Thanks a lot Iococo for your answer! Very helpful.
I just created a function coupled with a “timer.performWithDelay” and now, time is running exactly like I wanted.
[lua]local t1 = system.getTimer()

local function runningTime(event)
local t2 = system.getTimer()
local ms = t2 - t1
print("elapsed milliseconds = " … ms)
t1 = ms - t2

local floor = math.floor
local seconds = floor(ms / 1000)
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)
local hours = floor(minutes / 60); minutes = floor(minutes % 60)

formattedTime = string.format("%02d:%02d:%02d", hours, minutes, seconds)
print("elapsed time = " … formattedTime)

end
timer1 = timer.performWithDelay(1000, runningTime, 0)[/lua]
[import]uid: 8970 topic_id: 5924 reply_id: 20990[/import]

Thank you for posting this code, its been helpful for me. one question, do either of you guys know how to format the milliseconds? In my game i also have the format 00:00:00, but i am doing minutes,seconds,milliseconds. So i have it displaying correctly but the milliseconds on the end are like 4 digits, and grows as time goes. here is what i have

  
local function refreshtime()  
local newfresh = getCountedTime( raceTimer )  
  
local floor = math.floor  
  
local ms = newfresh  
local seconds = floor(ms / 1000)  
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)  
formattedTime = string.format("%02d:%02d:%02d", minutes, seconds, ms)  
  
mytime.text = formattedTime  
end  
  

i dont understand the syntax for string formatting at all so i cant figure it out. any way i could also just cut off the milliseconds so it only shows 2 digits of the milliseconds?
thanks for your help [import]uid: 19620 topic_id: 5924 reply_id: 29878[/import]

If you want to understand how the formatting works, you can look up “printf” on Google. Although it differs in many particulars, it’s similar in concept.

If you’re interested in just showing tenths and hundredths of seconds, that’s easy enough to do. First what you want is the number of milliseconds from 0-999. If you use “ms” the way you have it above, you’re displaying the total milliseconds elapsed, which is not what you want. You want the number of milliseconds between the two closest integer number of seconds. Like this:

local ms = floor(newfresh % 1000)  

If you display the value of “ms” now, it will be 0-999, but you want 0-99, just two digits. So divide it by 10:

local hundredths = floor(ms / 10)  

Now you can display 3 minutes, 4.56 seconds as 03:04.56 like this:

mytime.text = string.format("%02d:%02d.%02d", minutes, seconds, hundredths)  

(I haven’t tested any of this code: I just wrote it out, so there may be some mistakes.)
[import]uid: 9659 topic_id: 5924 reply_id: 29883[/import]

Ok so using the above code my milliseconds now stay to 2 digits, but it never displays seconds or minutes, it continually counts up on the milliseconds “hundredths” and then restarts… ill try and see whats going on, let me know what you think. thanks [import]uid: 19620 topic_id: 5924 reply_id: 29884[/import]

I was just giving you the “milliseconds” part of the code. You still need the “minutes” and “seconds” part. :slight_smile:
[import]uid: 9659 topic_id: 5924 reply_id: 29886[/import]

well here is what i have now:

  
local function refreshtime()  
  
local newfresh = getCountedTime( raceTimer )  
  
local floor = math.floor  
  
local ms = floor(newfresh % 1000)  
local hundredths = floor(ms / 10)  
local seconds = floor(ms / 1000)  
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)  
formattedTime = string.format("%02d:%02d:%02d", minutes, seconds, hundredths)  
  
mytime.text = formattedTime  
  
end  
  

do i need to apply the % 1000, to seconds and minutes also? sorry this is just one area of coding that i dont excel at. [import]uid: 19620 topic_id: 5924 reply_id: 29887[/import]

You need to change line 9 to:

local seconds = floor(newfresh / 1000)  

[import]uid: 9659 topic_id: 5924 reply_id: 29890[/import]

Works like a charm, thanks lococo, for anyone else that this might help, here is the working code:

  
local function refreshtime()  
  
local newfresh = getCountedTime( raceTimer )  
  
local floor = math.floor  
  
local ms = floor(newfresh % 1000)  
local hundredths = floor(ms / 10)  
local seconds = floor(newfresh / 1000)  
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)  
formattedTime = string.format("%02d:%02d:%02d", minutes, seconds, hundredths)  
  
mytime.text = formattedTime  
  
end  
  

By the way im using the Beebe class to start and count my game counter. [import]uid: 19620 topic_id: 5924 reply_id: 29892[/import]

I don’t know what I’m doing wrong, but I can’t get this to display on the screen at all. I’ve required beebeclass.lua.
[lua]local function refreshtime()

local newfresh = getCountedTime( raceTimer )

local floor = math.floor

local ms = floor(newfresh % 1000)
local hundredths = floor(ms / 10)
local seconds = floor(newfresh / 1000)
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)
formattedTime = string.format("%02d:%02d:%02d", minutes, seconds, hundredths)

mytime.text = formattedTime

local timeText = display.newRetinaText(mytime.text, 240, 30, “Helvetica”, 28)

end[/lua]

I’ve been stuck on this for a while now and I just can’t figure it out. Thanks [import]uid: 46082 topic_id: 5924 reply_id: 98416[/import]