Seconds Dif From Now

The SecondsDifFromNow function calculates the difference in seconds between a given date-time and the current system date-time. It is useful for precise, second-level time comparisons in real-time operations, validations, or monitoring workflows.

When to Use

Use SecondsDifFromNow when you need to:

  • Validate the exact seconds elapsed since a particular event.
  • Track response timeouts or expiry windows.
  • Monitor real-time system events that are sensitive to second-level differences.

It is ideal for scenarios where even minor time gaps matter, such as banking transactions, OTP validation, or system activity monitoring.

Common Use Cases

  • OTP Expiration:
    Check if an OTP has expired by verifying if more than 120 seconds have passed since issuance.
  • Session Timeout:
    Validate if a user session should be terminated after being idle for a certain number of seconds.
  • Real-Time Alerts:
    Trigger a system alert if a critical system event hasn’t occurred within a specified time in seconds.

Concept

You typically use the SecondsDifFromNow function inside the Smart Selector, which has different function tabs. By typing “SecondsDifFromNow” in the search bar, you can select it and define its parameters.

Parameters:

DatetimeThis defines the source date from which the function will calculate the difference in years compared to the current date.

You can enter:
– A reference to an element (e.g., DatePicker_StartDate)

– Or a JSON path to fetch a value from an entity (e.g., Insurance.policy[0].startDate)
FormatThis specifies the format of the date being read. The function uses this to correctly parse the input date before calculating the difference. DD, MM, YY, HH, SS, DDD, MMM, YYYY, etc.

Since this function can be applied across multiple scenarios to show year difference from the current year, we’ll walk through one specific example below:

Logout the User Session After 10 Minutes of Login
When a user logs into the application, their login timestamp is recorded. Using the SecondsDifFromNow function, the system checks if 3600 seconds (60 minutes) have passed since the recorded login time.
If 10 minutes are completed, the application will automatically logout the user session to maintain security. Then, the user must reauthenticate (e.g., with a PIN, password, or biometrics).

Pre-Requisite:

For implementing this scenario, you don’t need elements. Nevertheless, you have to do three steps:

  1. Creating an Entity
  2. Creating a Business Rule
  3. Creating a Task Group

Creating an Entity

Create a JSON entity that stores login time:

From here, the application will fetch the data and compare using the BR and the function.

Create a Business Rule

In the business rule, you will define a condition using the function. Create a BR (e.g., SecondDifFromNow) that checks if the user session exceeds 60 minutes.

Left SideOperatorRight Side
DescriptionSelect the SECONDSDIFFROMNOW function and define its parametersMore than or Equal (>=) Select the Static Function and define its parameter
ParametersDateTime: loginTimestamp attribute.

Format: DD-MM-YYYY HH:mm:ss
CastTo: Number

Value: 3600

Creating a Task Group

The task group defines what happens when user perform certain action:

  1. Create a Task Group to fetch time upon login and copy that to the attribute “loginTimestamp“. So, drag a copy data task and do the following:
Copy FromCopy To
1. Select the CURRENTDATETIME function.

2. Define its format (e.g., DD-MM-YYYY HH:mm:ss).
Select the attribute “loginTimestamp“.
  1. Create another task group, drag Session Validate Task to the execution panel and do the following:
Session TypeBusiness Rule
Select User Session to logout the session on the application and make the user enter the app-level password to re-enter the app.Select the BR created in the above step i.e., SecondDifFromNow.

Result

Now after login, the CURRENTDATETIME function will fetch the login time and save it to the entity named loginTimestamp. BR will continue to check whether 60 minutes have passed since login using the SECONDSDIFFROMNOW function. When 60 minutes are completed, the Session Validate task will log out the user.

Alternative Example: Logout After 10 Minutes of Inactivity

Instead of checking the time since login, you can log out the user after 10 minutes of inactivity. To achieve this, you’ll need to implement a custom JavaScript function that captures the last activity timestamp (e.g., on tap, click, swipe).

Steps Overview:

  1. Create Entity: Create an entity (e.g., SessionInfo) with an attribute like lastActiveTime to store the user’s last interaction time.
  2. Custom Function: Use JavaScript and make a custom function that notes user actions (click, scroll, keypress, etc.) and updates lastActiveTime each time there’s an interaction.
  3. Business Rule: Create a Business Rule using the SecondsDifFromNow function.
    SecondsDifFromNow(SessionInfo.lastActiveTime) >= 600
    → This checks if more than 600 seconds (10 mins) have passed.
  4. Task Group: Create a Task Group with:
    • A Copy Data task that copies the last interaction time using the custom function made in the step above and stores it to the lastActiveTime entity.
    • A Session Validate Task that logs out the user by checking the above Business Rule.

Best Practices:

  • Avoid comparing timestamps from different formats (e.g., comparing ISO with custom-formatted string) — this can lead to incorrect results.
  • Use entities to store timestamps like login time or last active time, so you can reference them reliably across sessions or conditions.
  • Validate user-entered dates using regex or format settings to prevent invalid input.
  • Reload elements when date values are updated during runtime to reflect the latest changes.
Was this article helpful?
YesNo