How do i get hours and minutes of seconds?

Hello, i am using a while wait(1) loop to get the seconds of the timer. But it doesn’t work correctly. The problem is in the Convert_To_HMS function.

My script:

local function Format(Int)
    --// HANDLER //--
    return string.format("%021i", Int)
end

local function Convert_To_HMS(Seconds)
    local Minutes = (Seconds - Seconds % 60) / 60 -- ?
    Seconds = Seconds - Minutes * 60 -- ?
    
    local Hours = (Minutes - Minutes % 60) / 60 -- ?
    Minutes = Minutes - Hours * 60 -- ?
    
    --// FROMAT //--
    return Format(Hours) .. ":" .. Format(Minutes) .. "" .. Format(Seconds)
end

local function Update_Timer(Player)
    local leaderstats = Player:WaitForChild("leaderstats")

    local Current_Time = leaderstats.TimePlayed
    Current_Time.Value += 1 -- Current_Time - StartTime
    
    --// STRING //--
    local Timer_String = Convert_To_HMS(Current_Time.Value)
    
    print("Formatted time: " .. Timer_String)
end

local function Start_Time(Player)
    while wait(1) do
        --// UPDATE //--
        Update_Timer(Player)

        RunService.Heartbeat:Wait()
    end
end

Start_Time(Player)
1 Like

I have a script, Try changing it a bit

function Format(Int)
	return string.format("%02i", Int)
end

function convertToHMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end
local player = game:GetService("Players").LocalPlayer
while task.wait() do
	script.Parent.Timetell.Text = convertToHMS(player:WaitForChild("leaderstats").times.Value)
end

I used ur format and convert function but output comes out as:

21:07:29.841 Formatted time: 00:0154 - Server - Handler:37

Weird.

Whats your current code now? this works for me…

It is supposed to work I try them on Roblox Studio.

local function Convert_To_HMS(Seconds)
	local Minutes = math.floor(Seconds / 60)
	Seconds = Seconds % 60

	local Hours = math.floor(Minutes / 60)
	Minutes = Minutes % 60

	--// FROMAT //--
	return string.format("%02d:%02d:%02d", Hours, Minutes, Seconds)
end
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.