In this tutorial we show how to display the current date and time on the visualization as well as different ways of formatting.
1. Clock update #
We will start from a small portion of Lua code, that will allow us to obtain the current date and save it in a VAR variable.
function clock()
set('VAR.clock',date())
end
The clock function invokes the Lua function- date It returns the current date and time and saves theset variable using the set function clk.
Date and time are displayed in the default format and should have the following form: :: <nazwa dnia> <nazwa miesiąca> <dzień> <godzina>:<minuta>:<sekunda> <rok>, e.g.: Wed Nov 7 14:50:00 2012.
2. Starting the clock #
The code mentioned in section 1. does not perform any action until the clock() function is invoked. In order to get the time and date automatically updated, create a timer that will periodically invoke the cclock() function. We will use timer.
- Add a new timer in Timers tab. You can add the description, e.g. „Clock” (optional).
- In Seconds field enter 0. The timer will be invoked every minute.
- Click on Add command, in order to define the function
clock(). In the displayed window in the Name field enterC.LOGICand in the Value field enter:clock().

From now on the clock will be updated every minute automatically.
3. Effect on the visualization #
In order to see the effect on the visualization, add the element like Text and in the Channel field enter the name of the VAR variable in which the date and time is saved. In this case it is VAR.clock.
4. Date and time formatting #
In this section we present the ways of formatting the date and time. The following table li- sts all the available parameters that can be used to format dates and times. Examples relate to the date of the 07.11.2012 (Wednesday) at 15:10:35. All parameters can be used with the date function mentioned above.
| Parameter | Description | Example/Syntax |
|---|---|---|
%a | Short name of the day | Wed |
%A | Full name of the day | Wednesday |
%b | Short name of the mouth | Nov |
%B | Full name of the month | November |
%c | Full name of the month | Wed Nov 7 15:26:16 2012 |
%d | Day of the month number | 07 |
%H | Time, (24-hour format) | 15 |
%I | Time (12 hour format) | 03 |
%M | Minute | 10 |
%m | Month | 11 |
%p | Time of the day (PM or AM) | PM |
%S | Second | 35 |
%w | Day of the week (0=Sunday,6=Saturday) | 3 |
%x | Data | 07/11/12 |
%X | Time | 15:10:35 |
%Y | Year(full figure) | 2012 |
%y | Year (two digits) | 12 |
If the date function is invoked without any parameter (like in code from chapter 1, the %c parameter is used by default.
4.1. Usage #
Below we present some examples of date and time formatting which can be used in the code from the first section of the tutorial.
| Function with formatting | Result |
|---|---|
date("%d-%m-%Y, %H:%M") | 07-11-2012, 15:10 |
date("Dziś jest: %d/%m/%Y, %H:%M") | Today is: 07/11/2012, 15:10 |
date("%A %d. %B %Y, %H:%M") | Wednesday 07. November 2012, 15:55 |
date("%H:%M") | 15:10 |