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

4

Attached Files

The following files have been attached to this tutorial:

.capx

easy-date-and-time.capx

Download now 171.25 KB

Stats

10,363 visits, 17,101 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 3 Apr, 2014. Last updated 19 Feb, 2019

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:

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:

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.

.CAPX

easy-date-and-time.capx

Download now 171.25 KB
  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • set h to :

    Browser.ExecJS("var today = new Date(); today.getHours();")

    set m to:

    Browser.ExecJS("var today = new Date(); today.getMinutes();")

    set s to:

    Browser.ExecJS("var today = new Date(); today.getSeconds();")

    set text to

    zeropad(h,2) &":"& zeropad(m,2) &":"& zeropad(s,2)

  • So, the usual JS way of getting hours and minutes cannot be used? like "Browser.ExecJS("Date.getHours()")"

    I tried this , but nothing returns