Jump to content

convert ms to minutes, seconds


mariogallardo

Recommended Posts

I'm trying to convert ms (2 minutes) to minutes and seconds in this format: 2:00

 

local timer = 120000
local minutes = 2
local seconds = 0
local milliseconds = 0

function convertTime ( ms ) 
    if ( ms and type ( ms ) == "number" ) then 
    local milliseconds = math.floor((ms % 1000) / 100)
    local seconds = math.floor((ms / 1000) % 60)
    local minutes = math.floor((ms / (1000 * 60)) % 60)
	return minutes, seconds, milliseconds
    end 
end 

setTimer (
function ( )
	timer = timer - 10
	minutes, seconds, milliseconds = convertTime(timer) 
end
,1000, 1 
) --- added a timer which deduct 1 ms from the 2 minutes timer every seconds

If I add milliseconds to the dxDrawText then it's display in this format: 1:599, 1:598, 1:597, 1:596 etc...
 

dxDrawText(minutes..":"..seconds..milliseconds, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)


But if I skip the milliseconds variable then it just display one zero at the start (2:0) and if reaches 1:0 I want to display in this format: 2:00, 1:00
 

dxDrawText(minutes..":"..seconds, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)

 

Link to comment
1 hour ago, mariogallardo said:


But if I skip the milliseconds variable then it just display one zero at the start (2:0) and if reaches 1:0 I want to display in this format: 2:00, 1:00
 

local text

if (seconds == 0) then
  text = minutes .. ":" .. seconds .. "0"
else
  text = minutes .. ":" .. seconds
end

dxDrawText(text, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)

???

What do you mean? Could you please explain in more detail? Doesn't the...

dxDrawText(minutes..":"..seconds..milliseconds, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)

... script already display as 2:00 if seconds == 0 and milliseconds == 0? What do you mean if it "reaches 1:0"????

Edited by The_GTA
Link to comment
22 minutes ago, The_GTA said:
local text

if (seconds == 0) then
  text = minutes .. ":" .. seconds .. "0"
else
  text = minutes .. ":" .. seconds
end

dxDrawText(text, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)

???

What do you mean? Could you please explain in more detail? Doesn't the...

dxDrawText(minutes..":"..seconds..milliseconds, screenW * 0.8443, screenH * 0.9148, screenW * 0.9740, screenH * 0.9769, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false)

... script already display as 2:00 if seconds == 0 and milliseconds == 0? What do you mean if it "reaches 1:0"????

Well, if the script displays (or reach I don't know how to say that in english, sorry) 1:09-1:01 then it's shows 1:9,1:8,1:7,1:6,1:5,1:4,1:3,1:2,1:1 instead of 1:09, 1:08, 1:07 etc... until 1:00 the same as 0:09, 0:08 shows 0:9, 0:8 instead of 0:09, 0:08 until 0:00

Link to comment
2 hours ago, mariogallardo said:

Well, if the script displays (or reach I don't know how to say that in english, sorry) 1:09-1:01 then it's shows 1:9,1:8,1:7,1:6,1:5,1:4,1:3,1:2,1:1 instead of 1:09, 1:08, 1:07 etc... until 1:00 the same as 0:09, 0:08 shows 0:9, 0:8 instead of 0:09, 0:08 until 0:00

I think I understand. You want to display the seconds as double-digits, right?

local seconds_text

if (seconds < 10) then
  seconds_text = "0" .. seconds
else
  seconds_text = tostring(seconds)
end

local minutes_and_seconds = minutes .. ":" .. seconds_text

This script puts the time by minutes and seconds always in the format M:SS (1:08, 1:45, 5:12, 0:00, etc). Provided that seconds and minutes are integers which are not negative. Use the "minutes_and_seconds" variable for your drawing code.

Edited by The_GTA
  • Thanks 1
Link to comment
41 minutes ago, The_GTA said:

I think I understand. You want to display the seconds as double-digits, right?

local seconds_text

if (seconds < 10) then
  seconds_text = "0" .. seconds
else
  seconds_text = tostring(seconds)
end

local minutes_and_seconds = minutes .. ":" .. seconds_text

This script puts the time by minutes and seconds always in the format M:SS (1:08, 1:45, 5:12, 0:00, etc). Provided that seconds and minutes are integers which are not negative. Use the "minutes_and_seconds" variable for your drawing code.

Yes, thank you so much

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...