Minutes Difference From Now

The MinutesDifFromNow function calculates the number of minutes that have passed between the current time and a specified date-time value.

When to Use

  • Real-time Validations: Use this function to handle logic based on recent actions (e.g., OTP expiry, inactivity timers).
  • Timeout Management: Set up flows that expire or change behavior after a specific number of minutes.
  • Security Control: Track session inactivity or sensitive action delays for better security handling.

Common Use Cases

  • OTP Expiry Logic: Check if more than 3 minutes have passed since OTP was generated, and invalidate it if expired.
  • Inactivity Timeout: Lock app screen or log out users after 10 minutes of inactivity.
  • Wait Window for Edits: Allow changes to a submitted form only within 5 minutes of submission.
  • Short-Term Eligibility Check: Trigger next steps only after a defined waiting period (e.g., a 15-minute cooling-off).

Concept

You will typically use the function within the Smart Selector, which has various function tabs. By typing “MINUTESDIFFROMNOW” 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:

Scenario: Disable the “Resend OTP” button until 3 minutes have passed since the first OTP was sent.

Pre-Requisite:

  • Basic Understanding of Tasks and Entity.
  • Following UI Elements:
ElementReference NameDescription
ButtonButton__MinDiff_SubmitfomWhen the user clicks the Submit Form button, they get an OTP.
OTPOTP_35176_MINSDIFUser inputs an OTP under ‘Enter OTP‘ that they receive on their device.
ButtonButton_89473_MinsDiffResend OTP‘ button to remain disabled for 3 mins after the last OTP sent.

You have to do four steps:

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

Create an Entity

Create a JSON entity (e.g. ‘MinutesDifFromNow‘) to store the time when the OTP was sent:

This entity will store the timestamp when Send OTP is clicked.

Create Business Rule

Left SideOperatorRight Side
DescriptionSelect the MINUTESDIFFROMNOW function and define its parametersSmaller than or Equal (<=) Select the Static Function and define its parameter
ParametersDateTime: issuedat attribute.

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

Value: 3

Create a Task Group

The task group defines what happens when the user performs certain action:

  1. Create a Task Group, e.g. MinsDiff
  2. Drag the Enable/Disable task to the execution box:
    • Disable Elements: Select the Resend Button element that remains disabled for 3 minutes after the last OTP is generated upon clicking the Submit Form button.
    • Business Rule: Select the Business Rule created above (e.g., MinsDifFromNow).

Suggestion: You can implement a Copy Data task on the onClick event of the Resend OTP button. This task should use the CURRENTDATETIME function to capture the exact time the button is clicked and copy it to the issuedAt attribute in the MinutesDifFromNow entity. This ensures that the 3-minute cooldown resets every time the Resend OTP button is used.

Link Element and Task

Linking the task to the element through an event is crucial.

  1. Select Submit Form (with reference name Button__MinDiff_Submitfom).
  2. On the bottom section of the right panel, locate Events ().
  3. In the event section (), onClick event appears.
  4. Click the plus sign () adjacent to onClick.
  5. The event configuration opens. Drag the MinsDiff Task to the execution panel.
  6. Click the Save button located on the bottom right.

Note: You can also add a visible task on the onClick event of the Submit Form button if you want to keep the OTP element and Resend OTP button element invisible until the Submit Form button is clicked.

Result

Once implemented:

  • When the user clicks the Submit Form button, the current timestamp is saved as issuedAt.
  • The Resend OTP button remains disabled for 3 minutes after this timestamp.
  • If the user does not enter the OTP within 3 minutes, the Resend OTP button becomes enabled automatically.
  • If you follow the suggestion:
    • If the user clicks the Resend OTP button, the timestamp (issuedAt) is updated, and the button gets disabled again for another 3 minutes.

Best Practices

  • Use a consistent datetime format: Always use the same format (e.g., DD-MM-YYYY HH:mm:ss) when storing or comparing datetime values.
  • Handle repeated clicks gracefully: Prevent multiple rapid clicks on the Resend OTP button by immediately disabling it upon click.
  • Keep entity names meaningful: Use intuitive names like MinutesDifFromNow or OtpIssuedAt for easier maintenance and readability.
  • Avoid hardcoded time values: If your application allows dynamic configuration, keep the “3-minute” threshold in a config entity or constant for easy future updates.
  • Account for user inactivity: Optionally combine with session validation if the user remains idle beyond the threshold.
  • Ensure visual feedback: Clearly indicate to users why the Resend OTP button is disabled (e.g., with a timer or message).
Was this article helpful?
YesNo