[Solved] Digital clock / Leading "0" in minutes

Hi, I would like to display a digital clock in my project.
I’ve made a text object with this value: ToString(Time("hour"))+":"+ToString(Time("min"))
However the minutes from 0 to 9 have no leading “0”, so I get values like 8:0 and 16:1.

How do I fix it?

This works:
just add javascript code to the project. do you know how to do it
var time_var = runtimeScene.getVariables().get(“time_var”);

var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();

if (sec < 10) {
time_var.setString(hour+":"+min+ “:0”+sec);
}else if (min < 10) {
time_var.setString(hour+":0"+min+ “:”+sec);
}else {
time_var.setString(hour+":"+min+ “:”+sec);
}

1 Like

If seconds is less than 10 then a zero is added.

p.s. don’t forget to add external link

Thank you Markus_Schuetz.
I don’t know Javascript, so I’ll wait some time to see if anybody answers with a simpler solution.
I don’t really think it will happen, so probably I’ll use your code (I think I’ll be able to do it with guides like this), so thank you :slight_smile:

Hi, there’s at least another solution that doesn’t involve Javascript or external event. But I would not call it easier. I used it before because I don’t know Javascript

The thing is to have a text variable “zeroornot” set at the beginning of the game, like this :
zeroornot.0 = “0”
zeroornot.1 = “”
zeroornot.2 = “”
zeroornot.3 = “”
zeroornot.4 = “”
zeroornot.5 = “”

The following expression will return “0” if Time(min) <10, else it would return nothing ("") :

  • zeroornot[ToString(Floor(Time(“min”)/10))]

So you just have to add it in your time display text :

ToString(Time(“hour”))+":"+VariableString(zeroornot[ToString(Floor(Time(“min”)/10))) +ToString(Time(“min”))

Check out this thread…hopefully it can help!

By writing to you, I found that my code contained an error.
Many thanks for that. I’ve got the code correct now.

if ((sec < 10)&&(min < 10)) {
time_var.setString(hour+":0"+min+ “:0”+sec);
}else if ((sec >= 10)&&(min < 10)){
time_var.setString(hour+":0"+min+ “:”+sec);
}else if ((sec < 10)&&(min >= 10)){
time_var.setString(hour+":"+min+ “:0”+sec);
}else {
time_var.setString(hour+":"+min+ “:”+sec);
}

To use this code simply do the following:

  • Click on the project manager icon on the left

  • add external event

  • rename and click

  • select a scene where the time is to be used

  • click the plus symbol

  • Javascript code

  • just delete the first line (“runtimeScene.setBackgroundColor(100,100,240);”

  • paste the code, save as a precaution

  • in the scene in events - again via the plus symbol - click on the link external events

  • click on the newly created line and click on the event just designed.

-now only put the text on the variable
(txt change the text of xxx: set to VariableString(time_var)

Then it should work. Hope I could help. Without your thread I would have missed my mistake. Thanks.

The lines above the if query are of course part of the code, sorry.

So here is the complete code:

var time_var = runtimeScene.getVariables().get(“time_var”);

var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();

//20:01:09
if ((sec < 10)&&(min < 10)) {
time_var.setString(hour+":0"+min+ “:0”+sec);
}else if ((sec >= 10)&&(min < 10)){
time_var.setString(hour+":0"+min+ “:”+sec);
}else if ((sec < 10)&&(min >= 10)){
time_var.setString(hour+":"+min+ “:0”+sec);
}else {
time_var.setString(hour+":"+min+ “:”+sec);
}

1 Like

Extending @Markus_Schuetz’s solution, here’s a simple way using JavaScript. It sets the scene variable formatted_time, creating the variable if it doesn’t already exist :


The JavaSctipt code :

function right(str, number) {
return str.slice(-number);
}

var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();

runtimeScene.getVariables().get("formatted_time").setString(right("00" + hour, 2) + ":" + right("00" + min, 2) + ":" + right("00" + sec, 2));
1 Like

Hi I don’t know if this fits to your needs but there is this extension to format

1 Like

Wow, I did not think I would receive so many helpful solutions; there’s something I can learn from each answer, thank you all!

In the end I was inspired by Bluzima and did this:

I haven’t found a way to put if Time("min") < 10 as a condition. Is it possible?

1 Like

There’s a condition called “compare 2 numbers”, or so.

2 Likes

Thanks! Final form of my code:

2 Likes

Ah, good.
Did you do the same with
the seconds too?
Well done.

I don’t need to show seconds but I guess the idea is the same, but with Time("sec")