Display a variable of 0 as "00"

This may seem like an odd question. I'm trying to implement a calendar into a text adventure game I am building, where the minutes and hours change depending on what the character does. So far I have been able to implement this rather well using variables, and I have been able to use HUD Maker to display a 24-hour clock based on these variables: a variable for days, one for hours, and another one for minutes.

The problem, however, is that single digit variables only display as just that, single digits. So any hour or minute below 10 would be rendered like 2:0 or 4:5, instead of 02:00 or 04:05 like in a standard 24-hour clock.

The only way that I can see to possibly to display the variables like that would be to set up a conditional branch and have a text string variable of the proper 24-hour clock style display for each minute and hour. But with 60 minutes in an hour and 24 hours in a day, this sounds like it would be a lot of work, and I'm wondering if there isn't a simpler way to go about this, either in the editor itself or with JavaScript.
You can use a Javascript function padStart like this:
Code:
num.toString().padStart(2, '0');

Calling that function on a number will return a string with the format that you need:
Code:
const num1 = 11;
const num2 = 5;
num1.toString().padStart(2, '0'); // displays "11"
num2.toString().padStart(2, '0'); // displays "05"
Awesome! How do I use this with a variable? Do I use the script operand and plug that into the variable or do I have to do something like a script call? Sorry, I really wish I knew more how to use JavaScript.
Are you storing the number of hours in a game variable right now, like, say variable #7?

This is the general code if you have 'hours' in game variable #7
Code:
$gameVariables.value(7).toString().padStart(2, '0')

What does it do?
Code:
$gameVariables  // all the variables
.value(7)  // get the value of variable 7 - at 3am this value is the number 3
.toString() // turn the number 3 into the string "3". A string is text, not a number
.padStart(2, '0') // pad the start of the string until it has a length 2. Pad with the text "0"

2 ways to integrate it to your HUD:

1.) Your HUD maker might allow you to put Javascript in? I've always built my own HUDs, so I'm not familiar with the popular HUD makers. If so, you may be able to put that code above directly in the settings of the plugin.

2.) In your clock event, you can keep the hours in the "Hours" variable and then store the "text representation" in a "Hours Text" variable.
upload_2019-1-15_21-4-41.png

Then your HUD maker you can display the "Hours Text" variable.

Note! Never do any calculations with variable #8, because it is only text. You'll get weird results if you try to add, subtract, or compare (like V[8] < 12 means it is morning) because it is text, not a number.
Yes, the HUD maker I am using allows implementation of JavaScript. I got it all working, thanks to your help! You are awesome!
Hello, I am trying to do the same thing, but I keep getting the error "Undefined is not an option".
What am I doing wrong? Can you help me?
Thanks.

@Sarry123 , please refrain from necro-posting in a thread. Necro-posting is posting in a thread that has not had posting activity in over 30 days. You can review our forum rules here. Thank you.


I'm closing this thread.

Original thread: https://forums.rpgmakerweb.com/threads/display-a-variable-of-0-as-00.104496/