Clock with Date and Time

Hi Folks,
I am interested in adding a clock to my game displaying the current Time (am/pm) with no seconds just hours and minutes, and the current date (day,month, year).

But I am unsure on how to do it, and have it update whilst in game.

Any help appreciated.

thanks

MobileNoob

There are some expressions to do it:


You can save the values in some variables as I’m doing in the image, then use the variables to change a text object string or something :slight_smile:

:exclamation: :exclamation: :exclamation: If you are using the web platform (HTML5), take care of the functions “mday” and “wday” because they are broken in that platform, if you need them just modify the file located at:

GDevelopInstallationDirectory/JsPlatform/Runtime/events-tools/runtimescenetools.js

Change the line 111:

return now.getdate();        >>       return now.getDate();

and the line 117:

return now.getday();        >>       return now.getDay();

Notice it’s just a lowcase > uppercase letter change :slight_smile:

Thanks for the reply Liz.

Was a bit confused about Month though, because

Do = Time(mon) to Global Variable Month

returns 0 for January.

I tried to add 1 to Global Variable Month

Do = GlobalVariable(Month) + 1 to Global Variable MonthAdj

Edit: That in fact worked for Month and similarly for year by adding 1900.

However with time 11:06 displays as 11:6 → any ideas ?
Edit Solution attached in Screen grab → is this the best way ?

And how do I get an am/pm rather than 24hour clock ?

thanks for the tip on the js file have edited it.

Right now you are adding a “0” before the minutes, no matter the minutes value. You should check if minutes is < 10, if so add a “0” before, this way you convert 1, 2, 3 into 01, 02, 03. But don’t add a “0” if minutes is >= 10, otherwise you’ll convert 12 into 012 :slight_smile:

[code]Conditions: No conditions (always)
Actions: Do = Time(min) to global variable Minute
Do = GlobalVariableString(Minute) to the text of global variable MinuteString

Conditions: Global variable Minute is < 10
Actions: Do = “0” + GlobalVariableString(Minute) to the text of global variable MinuteString[/code]
Now the variable MinuteString has the value with the the extra “0” if needed, you can use the variable Minute in maths (because it’s a number), and use MinuteString to display it in text objects (you can’t use it for maths because it’s a string, “09” for example).

Thanks Liz.

I am working on a codebase for a clock as we speak.

I will upload it here as a resource when complete.

I am aiming for the following format;

10:23AM Saturday 10th of March 2018

The reason for the format is that I live in Australia (DD/MM/YY),
but the US format is (MM/DD/YY),
so by expressing it this way, it is in an International format.
Will let you know how I go, I am a slow coder so it may take a few days for me to perfect it !

MobileNoob

Although I do like the format on the Forum

Sat 10 Mar 2018, 12:44

:wink:

Here in Latin America we use the same format (DD/MM/YY). Of course I’m subjective, but this format is more “international” I think, and has more sense (day D of the month M of the year Y) :imp:

You can change the format in Account settings >> Board preferences, if you don’t like it anyway :wink:

Well it works, but it can probably be optimized better.

Mobile Noob
Clock.gdg (77.3 KB)

Hi Folks,
I am still having trouble on understanding how to anchor the clock to the right hand side of the screen.

eg; right justified 30pixels from the right of screen with a screen width of 600 pixels.

Any help appreciated.

thanks

MobileNoob

An action like this one just after setting the clock string should do the trick:

Thanks Heaps Liz

Hey Folks updated code for my dodgy clock.

clock_demo.jpg

12AM was displaying as 0AM

MobileNoob
ClockV2.gdg (83.4 KB)

1 Like

How do I add this in my game?

.gdg file is not compatible in GDevelop5. GDevelop4 used to save in .gdg file. That means, you can’t add it into your game.
I read it here.

So is there a way now to add actual date and time?

Wait. I will see it.

1 Like

Here is a video I found on YouTube:

1 Like