How to convert Game time Format to 00:00:00

Hi.

Anyone have an idea how to set the gametime to a 00:00:00 format
Basically i want it to be 00:01:08 indicating 1 minute 8 seconds.

Right now it outputs as 1.8
I tried various ways of doing this but none of them does what i want.
The only solution i do have is a very long a tedious and unpractical way of comparing every second.

Compare INT if equal to 1 set String to 01, If INT 2 Set String to 02 etc.

Im sure there is an easier way. Just haven’t found it yet.

My HUD is configured to print it correctly. I just need the correct format

You can get the seconds by using Modulo. In code you would use the % sign.

float seconds = GameTimeInSeconds % 60; // 60 because there are 60 seconds in a minute

The way it works is it will remove 60 (or whatever you use) from GameTimeInSeconds until there’s less then 60 left. It will then return whatever is left. In this case you remove one minute at a time until there’s less then a minute left, and the result is the remaining seconds.

You get the minutes by dividing by 60 and flooring the value.
You get the hours by dividing by (6060) which is 3600, and flooring the value.
You can get days by dividing by (60
60*24) and flooring the value.

Ahh thx i learned something new.
However it still does not add a 0 in front of seconds. It will still be 1,2,3 and not 01,02,03.

Sorry, how about this? I switched the variables to Ints as it was printing it with decimals.

Thx a million dude. The get substring is what i was looking for. Does exactly what i needed.

So you solved the issue i was having and an issue i was going to have with the % modulus tip. Thx again.

@Dieselhead

How do i mark your answer as the answer?

Thanks for this answer I found it really useful . Do you know if it is possible to calculate milliseconds as well ?

Stumbled across your post while I was trying to do something similar.
I figured out a great way to do it that doesn’t require a lot of effort! :slight_smile:

This answer is outdated. ue4 has FormatText and ToText advanced settings have minimum number of digits now. See @Divo’s answer.

Thank you @Divo! You saved the day!

If you only need (minutes:seconds:milliseconds), I have created a custom actor component that will output this format automatically. Simply add the component to any blueprint you want and call the function. Everything else is taken care of under the hood. Code is all “copy/paste” to be blueprint user friendly. If you need hours as well feel free to modify the code, it shouldn’t be too difficult.