Scripting

Time and Date Calculations

Various manipulation options are available.

To work with the current date/time, a data source of the type “time” must be created. Within Peakboard, times/dates are handled as strings, which means that a conversion via script is always necessary.

Calculate Time and Date

For example, if you want to subtract 7 days from the current date, the current date must be stored in a variable, for example ManipulatedTime, and then converted to a date format. Afterwards, -7 is subtracted using the date addition function. To obtain a human readable result, the result is then converted into a formatted string.

Below is the implementation with Peakboard Building Blocks.

Substract_Date

As well as with a regular Lua script.

local parsedDate = date.parse(data.ManipulatedTime, 'yyyyMMddHHmmss')
local adjustedDate = date.addday(parsedDate, -7)
data.ManipulatedTime = date.tostring(adjustedDate, 'yyyyMMddHHmmss')

Caculate Time and Date differences

For calculating a time difference, a similar approach is required. The subtraction function is used to subtract the current time from the target time. The result is output in seconds. To obtain a different interval, the result must be divided by, for example, 60 to get the difference in minutes. This is shown in the following screenshot using Peakboard Building Blocks.

Date_diff

Similarly, in the following script, the date string from the variable ManipulatedTime is converted into a date. The difference to the current date from the data source Source_TimeCurrent is then calculated and the result is converted into whole days.

local parsedDate = date.parse(data.ManipulatedTime, 'yyyyMMddHHmmss')
local dateDifference = date.difference(parsedDate, data.Source_TimeCurrent.getluadate())
data.TimeDifference = math.floor(((dateDifference / 60) / 60) / 24)

We are learning from you!

Did you find this article helpful?

Be enlightened!

Do you need more support?

Peakboard Youtube icon Visit our YouTube channel

Our numerous videos for beginners and advanced users explain exactly how to design your dashboard.

Peakboard Templates icon Visit the Peakboard Templates

Download our templates for various use cases for free.

Peakboard icon Visit www.peakboard.com

Find out all about Peakboard and browse our different use cases and success stories.