I am creating a timer system using the scoreboard.

I would like the timer to display on the actionbar title in the format HH:MM:SS.

Currently I already have command algorithms that will set the number of hours, minutes, and seconds from the total number of seconds.

This is the command I use to output the time:

/tellraw @a [{"score":{"name":"hrs","objective":"time"}}, {"text":":"}, {"score":{"name":"mins","objective":"time"}}, {"text":":"}, {"score":{"name":"secs","objective":"time"}}]

Although this command may look long, it is simply concatenating each value and separating them with colons.

But however, when the hours, mins, or seconds are less than 10, they are displayed like this: 0:4:13.

I would like to prepend every value less than 10 with a 0, so that it displays like this: 00:04:13.

The only way I can think of doing this is to test specifically for each case of which values are less than 10, and prepend those with a 0 before output.

But this will get repetitive as there are many different combinations, and I think I will have to include one test for each combination of which values are less than 10.

I think there may be an easier way that I am missing out on. What is it, if any?

  • I could have sworn that I answered essentially the same question before, but I can't find it anymore. You basically want something like "%02i" in programming languages. But that doesn't exist in Minecraft commands. – Fabian Röling May 25 '20 at 21:45

Your idea is already close to the best you can do.
Maybe it helps to store partial strings like "01:" elsewhere first and then concatenate those, that way you don't get exponentially more combinations to check for when you get more elements. But for your 3 elements and 8 combinations, it might not be worth it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.