Showing (HH:MM:SS) time format in textmeshpro?

Hello all!

I’m trying to record the real-life time that the player has spent in game, convert it into a string, and display it via TextMeshPro.

At the moment, I’ve set it up as follows:

using TMPro;

public TMP_Text file1Time;

public void recordTime() //triggered with a button for now just to test it
{
    myTime = Time.realtimeSinceStartup;
    file1Time.SetText(myTime.ToString("00:00:00"));
}

This initially looks fine, however, instead of going from 00:00:59 —> 00:01:00 it instead goes all the way to 00:00:99 before going to 00:01:00.

I tried looking for some documentation on ToString() but couldn’t find any leads as to what parameters I’m supposed to be using here. Any guidance would be greatly appreciated.

Thank you for your time!

I think you may want to use the TimeSpan struct and it’s various ToString formats.

float myTime = Time.realtimeSinceStartup;
string timeText = System.TimeSpan.FromSeconds(myTime).ToString("hh':'mm':'ss");

Formatting string might be wrong but that should put you on the right path.

I had tried TimeSpan before but I think I was formatting the ToString parameters incorrectly.
That worked like an absolute charm! Thank you!

1 Like