View Categories

Date and time on the visualization

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.

  1. Add a new timer in Timers tab. You can add the description, e.g. „Clock” (optional).
  2. In Seconds field enter 0. The timer will be invoked every minute.
  3. Click on Add command, in order to define the functionclock(). In the displayed window in the Name field enter C.LOGIC and 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.

ParameterDescriptionExample/Syntax
%aShort name of the dayWed
%AFull name of the dayWednesday
%bShort name of the mouthNov
%BFull name of the monthNovember
%cFull name of the monthWed Nov 7 15:26:16 2012
%dDay of the month number07
%HTime, (24-hour format)15
%ITime (12 hour format)03
%MMinute10
%mMonth11
%pTime of the day (PM or AM)PM
%SSecond35
%wDay of the week (0=Sunday,6=Saturday)3
%xData07/11/12
%XTime15:10:35
%YYear(full figure)2012
%yYear (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 formattingResult
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

Powered by BetterDocs