How to easily capture date and time and insert them in a project

Favourite 30 favourites
Tutorial written by EstudioCelula3Originally published on 3rd, April 2014 - 4 revisions

Introduction

This tutorial will show how to capture date and time from a PC ou mobile device and insert this data in your C2's project.

What is required?

You need to insert in your project the Browser object and to use the expression ExecJS("Date()").

The expression ExecJS runs a Javascript code and return a result if it is a string or number, and the code Date() returns the actual date and time like the example in the image below:

How date and time is captured with ExecJS(
The code always returns in the fomat Abbreviated Weekday, Abbreviated Month, Day of the Month, Hours:Minutes:Seconds.

How to use

You can to store date and time in a local or global variable using the action Set value and the expression Browser.ExecJS("Date()"). You can to show directly in a Text object with the action Set text.

Both in the Scirra Arcade as in the CAPX attached in this tutorial there is a simple example using the expression. Only 2 events were used:

Example of the use of the expression
The example shows continuously the current date and hour every second.

Limitations

The Javascript code just returns date and time as mentioned above. I tried to use other comands to capture only date or time, but Construct 2 alerted me about the risks in to use same codes and stopped the execution. In the manual there is a explanation about this.

For now, if you need to capture only date or time, use system expressions such as left and right.

Note: AllanR told me about the plugin of rexrainbow, System date and time. You can use it instead of this tutorial.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

1
Lordshiva1948 44.3k rep

Wonderful tutorial thanks for sharing

Friday, April 04, 2014 at 8:07:12 AM
1
AllanR 18.5k rep

There is also an excellent Date plugin by rexrainbow which gives you many options to format a Date, or convert to (or from) a Unix TimeStamp. I used that plugin to create a Calendar/scheduling app...

www.scirra.com/forum/plugin-system-date-and-time_t63492

Friday, April 04, 2014 at 11:34:03 PM
1
JuniorTabanez 1,304 rep

OMG This really i needed

good work

Saturday, April 05, 2014 at 5:35:28 AM
0
EstudioCelula3 1,955 rep

I didn't know this plugin. Very interesting.

Sunday, April 06, 2014 at 2:13:11 AM
1
AbelaNET 14.9k rep

Thanks for sharing. This is really a cool tip.

Tuesday, April 08, 2014 at 8:14:05 AM
1
TechnoTom 182 rep

Hey Everyone

If you are still looking for this, there is a way of getting just the time.

Use this code instead of Browser.ExecJS("Date();")

\/ \/ CODE \/ \/
Browser.ExecJS("var currentdate = new Date(); currentdate.getHours() ':' currentdate.getMinutes();")

Friday, July 08, 2016 at 9:18:01 PM
1
Leandrus 6,001 rep

Full DateTime example:

Browser.ExecJS("var currentdate = new Date(); currentdate.getFullYear() & '-' & (currentdate.getMonth() 1) & '-' & currentdate.getDate() & ' ' & currentdate.getHours() & ':' & currentdate.getMinutes() & ':' & currentdate.getSeconds();")

Note. The PLUS symbol can't be displayed in here. Instead is the AMPERSAND (&) symbol for the representation.

Wednesday, September 07, 2016 at 10:09:17 PM
0
Je Fawk 3,579 rep
Full DateTime example:

Browser.ExecJS("var currentdate = new Date(); currentdate.getFullYear() & '-' & (currentdate.getMonth() 1) & '-' & currentdate.getDate() & ' ' & currentdate.getHours() & ':' & currentdate.getMinutes() & ':' & currentdate.getSeconds();")

Note. The PLUS symbol can't be displayed in here. Instead is the AMPERSAND (&) symbol for the representation.
quoted from Leandrus

@Leandrus missed an &, thus a PLUS at currentdate.getMonth() 1

So this is correct: (currentdate.getMonth() PLUS 1)

I struggled a good 10 minutes trying to figure out why the month was 1 behind :P

Monday, November 07, 2016 at 5:47:25 PM
0
hars 181 rep

Bonjour,

Je commence seulement depuis hier à essayer d'utiliser "Construct 2".
Ne connaissant rien à la programmation, vos tutos me paraissent incompréhensible car vraiment néophytes :-(
Quelqu'un aurait-il la gentillesse de me donner plus de détails pour insérer une horloge dans mon projet.
J'ai vu le code qui m'intéresse (celui de TechnoTom) mais je ne sais pas comment l'utiliser !
Browser.ExecJS("var currentdate = new Date(); currentdate.getHours() ':' currentdate.getMinutes();")

Merci pour votre aide ;-)

Wednesday, February 22, 2017 at 8:58:57 AM
0
hars 181 rep

515/5000
Hello,

I only started yesterday trying to use "Construct 2".
Knowing nothing about programming, your tutos seem incomprehensible to me because I'm really neophytes :-(
Would someone be kind enough to give me more details to insert a clock into my project.
I have seen the code that interests me (that of TechnoTom) but I do not know how to use it!
Browser.ExecJS ("var currentdate = new Date (); currentdate.getHours () ':' currentdate.getMinutes ();")

Thank you for your help ;-)

Wednesday, February 22, 2017 at 9:12:47 AM
0
orinab 583 rep

Thanks!
I've used left(Browser.ExecJS("Date()"),3) to know the current day, then i save it in a variable and each time I check if the day is the same as the one saved. If it's not, I know it's the next day.

Monday, June 12, 2017 at 5:34:19 PM
0
orinab 583 rep
515/5000
Hello,

I only started yesterday trying to use "Construct 2".
Knowing nothing about programming, your tutos seem incomprehensible to me because I'm really neophytes :-(
Would someone be kind enough to give me more details to insert a clock into my project.
I have seen the code that interests me (that of TechnoTom) but I do not know how to use it!
Browser.ExecJS ("var currentdate = new Date (); currentdate.getHours () ':' currentdate.getMinutes ();")

Thank you for your help ;-)
quoted from hars

It's missing ' ' signs. put this into a text field for example:
Browser.ExecJS("var currentdate = new Date(); currentdate.getHours() ':' currentdate.getMinutes();")

Tuesday, June 13, 2017 at 9:12:33 AM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.