Possible to get the current time of day as a string?

I’m using Thunkable to control an Arduino app via bluetooth, and I’d like to be able to use the Thunkable app to set the time of the Arduino app.

Is it possible to get the current time of day as a text string in Thunkable?

Yes. You do this like so:

image

The pattern is there for you to define the format of the sting.

2 Likes

Aha, thank you, it works perfectly.

One thing I ultimately need to do in my app is get the UTC time, ideally as epoch time, aka seconds in the unix era. Looking at the docs here:

http://ai2.appinventor.mit.edu/reference/components/sensors.html#Clock

It says “Instants are assumed to be in the device’s local time zone. When they are converted to or from milliseconds, the milliseconds for a given Instance are calculated from January 1, 1970 in UTC (Greenwich Mean Time).”

Does that mean GMT/UTC is somehow accessible?

If so, any idea how?

From researching some more I see I need to make a call to the web api. For some reason these blocks aren’t working though, can anyone see why?

blocks%20(3) blocks%20(2)

These should make the text of button 17 be the response of the URL, but nothing is happening. Am I missing something?

And in the designer I have the URL of Web1 blank, “allow cookies” is blank, “save response” is checked. Is that correct?

Edit: Aha! I needed to uncheck “save response”. Works well now.

Android knows which time zone you are in, and therefore could derive UTC, but there is not widget in Thunkable that capture and pass this information back directly.
However, all the time conversion that are based on instant are UTC. And an instant is a long structure that mentions a lot of stuff (separated with coma, and each having the form “NAME=value”), including the offsets. Here is a block that captures the offset:

image

What the above does is take the clock current time, and same the instant in a variable so that we can process it. The first step is find where the “ZONE_OFFSET=” caption is located, as immediately following it will be the value of the time difference between your time zone and UTC, expressed in millisecond. In my case, the value reads -18000000, and 18 million millisecond is 5 hours, implying I have to subtract 5 hours form the UTC to get my local times, or conversely, to add 5 hours to my local time to get UTC.
Since this value would be different as a function of the time zone and could be expressed with a different number of digits, I need to locate where the number ends, and that it at the start of the daylight saving time delimiter. Note that this DST_OFFSET component will also need to be taken into account if required, since this is the last component, the ending delimiter to extract the value is a closing square bracket “]”.
Now, with the zone offset, and optionally the daylight saving time one, you could calculate the corresponding time in UTC. I would recommend that you use the “add seconds” (using the derived offset divided by 1000, evidently) instead of subtracting the number of hours, so as to leave the burden of figuring out the date cross over midnight to the system. No point in reinventing the wheel here.